点击一个按钮,谷歌地图应该至少适合3个离你当前位置点最近的标记
Google maps should fit at least 3 nearest markers to your current location point, by clicking a button
我有一个按钮,可以将您重定向到地图上的当前位置,但重定向到位置后,地图应该会自动缩放,您应该能够看到至少3个最近的标记。
这是我代码的一部分。它唯一能做的就是适合绑定到所有标记,但不会显示我的位置
$('a.geolocation').click(function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var bounds = new google.maps.LatLngBounds();
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
for (var i=0; i<storeLocator.markers.length; i++) {
if(storeLocator.markers[i].getVisible()) {
bounds.extend( storeLocator.markers[i].getPosition() );
}
}
map.setCenter(pos);
map.fitBounds(bounds);
});
}
});
发生的事情就是这样。您将地图的中心设置为用户的位置。但是,然后你让地图符合你的商店的边界,这将调整地图的中心,而不一定让你的用户的位置可见。
你所需要做的就是确保这个位置也在边界内。
navigator.geolocation.getCurrentPosition(function(position) {
var bounds = new google.maps.LatLngBounds();
var pos = new google.maps.LatLng(
position.coords.latitude,
position.coords.longitude
);
bounds.extend(pos);
for (var i=0; i<storeLocator.markers.length; i++) {
if(storeLocator.markers[i].getVisible()) {
bounds.extend( storeLocator.markers[i].getPosition() );
}
}
map.setCenter(pos);
map.fitBounds(bounds);
});
相关文章:
- 点击一个按钮,谷歌地图应该至少适合3个离你当前位置点最近的标记
- 在谷歌地图API JS V3上显示距离用户当前位置最近的多个标记,距离30公里
- 如何使图形上最近的四个点发光
- 如何使用jQuery或JavaScript从用户位置返回3个最近的位置
- 如何存储最近访问过我网站的 100 个 IP 地址
- 如何将最近几个月计算为一个时期,并通过javascript更新到图表
- 最近 12 个月在 Javascript 中
- 关于使用类名定位第一个上一个最近元素的问题
- 返回离用户位置(地理位置)最近的3个位置,并列出这些位置
- 如何在javascript中筛选日期以仅获取最近4个月的日期
- 使用多个选择器查找DOM中最近的元素
- 如何获得距离多个固定地点最近的学校和其他预定义的地名
- 错误:WebGL:超过了此主体的16个实时WebGL上下文,丢失了最近使用最少的一个
- 使用SQLite获取距离今天最近的5个日期
- 查找最近的最外层导航(多个嵌套导航元素)
- 选择Array中给定索引两侧最近的2个元素
- 在多个选择框中获取最近的选择
- 一直只显示10个最近的服务器发送的事件消息
- 我需要你帮我找到离中心最近的20个邻居一个核聚变层
- Angularjs从一个选择框中按月筛选最近12个月的数据