我只想在选定地图标记时更改其图标。
我在片段类中实现了GoogleMap.onMarkerclickListener类。

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.onmaclicklistener接口并创建一个私有变量来存储当前选定的标记:

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

07-28 10:22