【问题标题】:Current location of user and show nearby Google Marker around them?用户的当前位置并在他们周围显示附近的 Google 标记?
【发布时间】:2016-07-18 08:11:20
【问题描述】:
我在我的应用程序中的不同位置和街道上放置了多个 Google 标记:
googleMap.addMarker(new MarkerOptions()
.position(new LatLng(26.89209, 75.82759))
.title("Food"));
我想要的是只向用户显示他们当前位置附近的标记。例如,如果用户在 XYZ 街道,那么只有 XYZ 街道标记会显示
【问题讨论】:
标签:
android
google-maps
google-maps-api-3
【解决方案1】:
查看Viewport Marker Management 文档,该文档通过获取用户可见的地图的当前边界来工作,然后 - 基于地图的边界 - 向服务器发送查询以获取位于界限。
只加载方块中的标记,这样可以减少加载时间和
提高了性能,并且随着盒子的移动,只有需要的标记是
显示,其余的被删除。
首先,将侦听器附加到map idle 事件,使其在地图停止平移时执行名为showMarkers() 的函数。
google.maps.event.addListener(map, 'idle', showMarkers);
然后添加函数showMarkers,通过调用map.getBounds() 获取当前视口边界,该函数返回一个对象,该对象具有边界的NorthEast 和SouthWest LatLng 点。使用bounds 信息,您可以向您的服务器(最好使用 AJAX)发送请求以获取这些范围内的标记。
function showMarkers() {
var bounds = map.getBounds();
// Call you server with ajax passing it the bounds
// In the ajax callback delete the current markers and add new markers
}
使用新的标记列表,您可以删除地图上的当前标记marker.setMap(null),并添加新标记marker.setMap(map)。
希望这会有所帮助!