更改选中 Google 地图标记的图标

Changing the icon of Google Maps marker while it's selected

我只想在地图标记被选中时更改它的图标。

我在我的 Fragment class 中实现了 GoogleMap.OnMarkerClickListener class。

public class MapFragment extends SupportMapFragment implements GoogleMap.OnMarkerClickListener{

....

  @Override
  public boolean onMarkerClick(Marker marker) {   
    marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.placemarker_pressed));
    return false;
  }
}

单击时更改标记图标。未选中时如何设置回原来的图标?

同时实现GoogleMap.OnMapClickListener接口并创建一个私有变量来存储当前选择的标记:

private Marker mSelectedMarker;

然后:

@Override
public boolean onMarkerClick(Marker marker) {   
    if (null != mSelectedMarker) {
        mSelectedMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.placemarker));
    }
    mSelectedMarker = marker;
    mSelectedMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.placemarker_pressed));
    return false;
}

@Override
public boolean onMapClick(LatLng latLng) {
  if(null != mSelectedMarker) {
    mSelectedMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.placemarker));
  }
    mSelectedMarker = null;
}