如何在 android 中同时更改地图上所有标记的颜色?如何只更改部分标记的颜色?
How do I change the colour of all markers on a map at the same time in android? How do I only change the colour of some of the markers?
情况一:
假设我用蓝色标记填充地图:
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 种标记,蓝色和红色,由两个不同的 latLngs 列表创建。
//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)));
}
单击 red 标记时,我希望所有 blue 标记都变为黄色。我该怎么做?
您需要保留对标记的引用,然后在需要时修改它们。
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);
}
然后
@Override
public boolean onMarkerClick(Marker marker) {
//change marker colour to yellow
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
for(Marker otherMarker : mMarkers) {
otherMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
}
return false;
}
第二种情况的类似解决方法
您可以通过为所有带有颜色 ID 的标记保存一个列表来实现它。您可以将 List 与带有标记和 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));
}
您也可以使用 Enum 而不是 int 作为 colorId。您也可以使用 Pair 而不是 POJO。
情况一: 假设我用蓝色标记填充地图:
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 种标记,蓝色和红色,由两个不同的 latLngs 列表创建。
//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)));
}
单击 red 标记时,我希望所有 blue 标记都变为黄色。我该怎么做?
您需要保留对标记的引用,然后在需要时修改它们。
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);
}
然后
@Override
public boolean onMarkerClick(Marker marker) {
//change marker colour to yellow
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
for(Marker otherMarker : mMarkers) {
otherMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW));
}
return false;
}
第二种情况的类似解决方法
您可以通过为所有带有颜色 ID 的标记保存一个列表来实现它。您可以将 List 与带有标记和 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));
}
您也可以使用 Enum 而不是 int 作为 colorId。您也可以使用 Pair 而不是 POJO。