【问题标题】: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?如何在android中同时更改地图上所有标记的颜色?如何仅更改某些标记的颜色?
【发布时间】:2017-09-18 21:16:53
【问题描述】:

情况1: 假设我用蓝色标记填充地图:

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: 假设我有 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)));
 }

单击 red 标记时,我希望所有 blue 标记变为黄色。我该怎么做?

【问题讨论】:

    标签: java android google-maps-markers marker


    【解决方案1】:

    您需要保留对标记的引用,然后在需要时对其进行修改。

    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;
    }
    

    第二种情况的类似解决方法

    【讨论】:

    • 谢谢!这对我有用。我还假设 List mMarkers 你的意思是 List mMarkers.
    • 很高兴为您提供帮助!您关于列表类型的假设是正确的,现在已更正。
    【解决方案2】:

    您可以通过为所有带有颜色 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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 2011-01-28
      相关资源
      最近更新 更多