目前,默认情况下,点击标记时,地图会以标记为中心。有没有办法控制它,引入一些偏移值。我有一个弹出式信息窗口有时会高一点,我想定位地图,以免它在顶部被切断。
答案 0 :(得分:3)
您可能会覆盖GoogleMap标记点击事件并在那里调整相机。
例如
Maker lastOpened = null;
mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
public boolean onMarkerClick(Marker marker) {
// Check if there is an open info window
if (lastOpened != null) {
// Close the info window
lastOpened.hideInfoWindow();
// Is the marker the same marker that was already open
if (lastOpened.equals(marker)) {
// Nullify the lastOpened object
lastOpened = null;
// Return so that the info window isn't opened again
return true;
}
}
// Open the info window for the marker
marker.showInfoWindow();
// Re-assign the last opened such that we can close it later
lastOpened = marker;
// Get the markers current position
LatLng curMarkerPos = marker.getPosition();
// Use the markers position to get a new latlng to move the camera to such that it adjusts appropriately to your infowindows height (might be more or less then 0.3 and might need to subtract vs add this is just an example)
LatLng camMove = new LatLng(curMarkerPos.latitude + 0.3, curMarkerPos.longitude);
// Create a camera update with the new latlng to move to
CameraUpdate camUpdate = CameraUpdateFactory.newLatLng(camMove);
// Move the map to this position
mMap.moveCamera(camUpdate);
// Event was handled by our code do not launch default behaviour.
return true;
}
});
mMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
if (lastOpened != null) {
// Hide the last opened
lastOpened.hideInfoWindow();
// Nullify lastOpened
lastOpened == null;
}
// Move the camera to the new position
final CameraPosition cameraPosition = new CameraPosition.Builder().target(point).zoom(mMap.getCameraPosition().zoom).build();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
});
此代码尚未经过测试,但至少应该为您提供一个良好的开端。 onMarkerClick的默认行为是移动相机并打开信息窗口。因此,覆盖它并实现自己应该可以让你随意移动相机。
谢谢,DMan
答案 1 :(得分:3)
我修改了上面非常好的答案,对于那些只想删除标记点击的移动到中心默认行为的人来说:
将此添加到setUpMap():
mMap.setOnMarkerClickListener(getMarkerClickListener());
然后添加方法:
Marker lastOpened = null;
public OnMarkerClickListener getMarkerClickListener() {
return new OnMarkerClickListener() {
public boolean onMarkerClick(Marker marker) {
if (lastOpened != null) {
lastOpened.hideInfoWindow();
if (lastOpened.equals(marker)) {
lastOpened = null;
return true;
}
}
marker.showInfoWindow();
lastOpened = marker;
return true;
}
};
}