Java 如何在android中同时更改地图上所有标记的颜色?如何仅更改某些标记的颜色?
情景1: 假设我使用蓝色标记填充地图,如下所示:Java 如何在android中同时更改地图上所有标记的颜色?如何仅更改某些标记的颜色?,java,android,google-maps-markers,marker,Java,Android,Google Maps Markers,Marker,情景1: 假设我使用蓝色标记填充地图,如下所示: for (LatLng latLng : latLngList) { mMap.addMarker(new MarkerOptions() .position(latLng) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE))); } 单击标记后,我希望将地图上每个标记的颜
for (LatLng latLng : latLngList) {
mMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
}
单击标记后,我希望将地图上每个标记的颜色更改为黄色。我该怎么做
目前,我只能使用此方法更改单击的特定标记的颜色:
@Override
public boolean onMarkerClick(Marker marker) {
//change marker colour to yellow
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
return false;
}
情景2:
假设我有两种标记,蓝色和红色,从两个不同的latlng列表创建
//create blue markers
for (LatLng latLng : latLngListBlue) {
mMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
}
//create red markers
for (LatLng latLng : latLngListRed) {
mMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
单击红色标记时,我希望所有蓝色标记都变为黄色。我该怎么做?您需要保留对标记的引用,然后在需要时修改它们
List<Marker> mMarkers = new Arraylist<Marker>();
for (LatLng latLng : latLngList) {
Marker marker = mMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
mMarkers.add(marker);
}
第二种情况的类似解决方法您可以通过使用颜色id保存所有标记的列表来实现。您可以使用带有标记和Id的POJO的列表,如下所示:
public class MarkerWithColor {
private Marker marker;
private int colorId; // red = 0, green = 1, blue = 2
public MarkerWithColor(Marker marker, int colorId) {
this.marker = marker;
this.colorId = colorId;
}
// getter
// setter
}
然后,每次添加标记时,创建pojo并保存到列表中:
List<MarkerWithColor> markerWithColors = new ArrayList<>();
// adding blue marker
for (LatLng latLng : latLngList) {
Marker marker = mMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
// add to the list
markerWithColors.add(new MarkerWithColor(marker, 2)); // 2 is blue color
}
/* Do the same for the red and green */
// Now you can change the specific color
// Change blue to yellow
for(int i = 0; i < markerWithColors.size(); i++) {
MarkerWithColor markerWithColor = markerWithColors.get(i);
markerWithColor.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
}
List markerWithColors=new ArrayList();
//添加蓝色标记
用于(LatLng LatLng:latLngList){
Marker Marker=mMap.addMarker(新MarkerOptions()
.位置(车床)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
//添加到列表中
添加(新的MarkerWithColor(marker,2));//2是蓝色
}
/*对红色和绿色做同样的处理*/
//现在您可以更改特定的颜色
//把蓝色换成黄色
对于(int i=0;i
您还可以对colorId使用Enum而不是int。您也可以用它代替POJO。谢谢!这对我有用。我还假设你所说的列表营销员是指列表营销员。很高兴为你提供帮助!您关于列表类型的假设是正确的,现在更正。
List<MarkerWithColor> markerWithColors = new ArrayList<>();
// adding blue marker
for (LatLng latLng : latLngList) {
Marker marker = mMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
// add to the list
markerWithColors.add(new MarkerWithColor(marker, 2)); // 2 is blue color
}
/* Do the same for the red and green */
// Now you can change the specific color
// Change blue to yellow
for(int i = 0; i < markerWithColors.size(); i++) {
MarkerWithColor markerWithColor = markerWithColors.get(i);
markerWithColor.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
}