javascript - 如何在 Google Maps V3 上居中和缩放多个标记

标签 javascript google-maps google-maps-api-3

我使用以下代码:

 var geocoder;
    var map;
    var markers = [];
    function initialize() {
        geocoder = new google.maps.Geocoder();
        // var latlng = new google.maps.LatLng(51.733833,0.351563);
        var latlng = new google.maps.LatLng(53.590875,-2.279663);
        // var latlng = new Array (new google.maps.LatLng (52.537,-2.061), new google.maps.LatLng (52.564,-2.017));
        var myOptions = {
            zoom: 7,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        addPostCode('EX4 5SP');
        addPostCode('EX16 6LH');
        addPostCode('M1 2AP');

}

我在下面的代码中找到了一些如何实现这个的例子:Google MAP API v3: Center & Zoom on displayed markers为此,使用了以下代码。

// map: an instance of GMap3
// latlng: an array of instances of GLatLng
var latlngbounds = new google.maps.LatLngBounds();
latlng.each(function(n){
   latlngbounds.extend(n);
});
map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds); 

问题是我需要使用邮政编码在 map 上显示位置。是否可以从邮政编码创建一个 GLatLng 实例数组,然后使用上面的代码缩放 map 上的多个标记并将其居中?

最佳答案

GClientGeocoder 可以获取邮政编码并返回 GLatLng:参见 https://groups.google.com/forum/#!topic/google-maps-api/JN8OW5Tyaws .

要使多个标记居中,请参阅 Find center of multiple locations in Google Maps

是的,这是 V2 -- GLatLng 把我扔了。 V3 中存在相同的功能: https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

关于javascript - 如何在 Google Maps V3 上居中和缩放多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20469749/

相关文章:

java - 未获取位置时关闭应用程序的对话框

encoding - Google map API - 几何编码 - TypeError : a. lat 不是函数

javascript - 如何比较两个函数中定义的两个JavaScript变量值

javascript - 如何将字符串数据发送到javascript?

javascript - 使用 JavaScript SDK 邀请 friend 加入 Facebook

google-maps - 谷歌地理编码 API 不如使用地址的谷歌地图准确

javascript - InfoWindow 不想使用 Google Maps Api V3 关闭

javascript - md-checkbox 的行为类似于单选按钮 - AngularJS

javascript - 在页面显示后加载 Google Maps API

javascript - 谷歌地图 API 返回 "Undefined"