Google MAP API v3:中心&放大显示的标记 [英] Google MAP API v3: Center & Zoom on displayed markers

查看:105
本文介绍了Google MAP API v3:中心&放大显示的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码在我的地图上设置标记:

I use the following code to set markers on my map:

  var latLngs = []
  $.each(locations.markers, function(i, m){
   var myLatLng = new google.maps.LatLng(m.latitude, m.longitude);
   latLngs[i] = myLatLng                                          
   var marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    shadow: shadow,
    icon: image,
    shape: shape,
    title: m.city,
    zIndex: i
   });
  }) 

标记显示在我的地图上。现在我想中心&缩放这些标记上的地图。我怎样才能做到这一点?
我曾尝试过:

The markers show up on my map. Now I would like to center & zoom the map on these markers. How can I accomplish this? I have tried:

map.fitBounds(getBoundsForLatLngs(latLngs));



的console.log给出了:

The console.log of latLngs gives out:

 [(46.793182, 7.146903) { b=46.793182,  more...}, (46.8077779, 7.1709386) { b=46.8077779,  more...}]

但它似乎不起作用,并且在控制台中没有出错。我在做什么错了?

But it doesn't seem to work, and I get no error in the console. What am I doing wrong?

推荐答案

我也发现这个修复缩放以适应所有标记

LatLngList: latLng ,例如:

LatLngList: an array of instances of latLng, for example:

// "map" is an instance of GMap3

var LatLngList = [
                     new google.maps.LatLng (52.537,-2.061), 
                     new google.maps.LatLng (52.564,-2.017)
                 ],
    latlngbounds = new google.maps.LatLngBounds();

LatLngList.forEach(function(latLng){
   latlngbounds.extend(latLng);
});

// or with ES6:
// for( var latLng of LatLngList)
//    latlngbounds.extend(latLng);

map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds); 

这篇关于Google MAP API v3:中心&放大显示的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆