gpt4 book ai didi

google-maps-markers - 在 Android Maps V2 中,有没有办法在点击标记时控制 map 的定位?

转载 作者:行者123 更新时间:2023-12-01 12:49:24 26 4
gpt4 key购买 nike

目前,默认情况下,点击标记时, map 会以标记为中心。有没有办法控制它,引入一些偏移值。我有一个弹出信息窗口,有时会高一点,我想定位 map ,以免它在顶部被截断。

最佳答案

您可能会覆盖 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

关于google-maps-markers - 在 Android Maps V2 中,有没有办法在点击标记时控制 map 的定位?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14517897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com