簡體   English   中英

如何在Google Maps V3上居中和縮放多個標記

[英]How to center and zoom multiple markers on Google Maps V3

我使用以下代碼:

 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:中心和縮放顯示標記以實現此目的,使用以下代碼。

// 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); 

問題是我需要使用Postcodes來顯示地圖上的位置。 是否可以從郵政編碼創建GLatLng實例數組,然后使用上面的代碼縮放和居中地圖上的多個標記?

GClientGeocoder可以獲取郵政編碼並返回GLatLng:請參閱https://groups.google.com/forum/#!topic/google-maps-api/JN8OW5Tyaws

有關居中多個標記的信息,請參閱在Google地圖中查找多個位置的中心

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

// locationsColl包含坐標列表

var latlng = [];
_.forEach(locationsColl, function (address) {
    latlng.push(address);
});

var latlngbounds = new google.maps.LatLngBounds();
_.forEach(latlng,function (n) {
    latlngbounds.extend(n);
});
// center map to central point between markers
map.setCenter(latlngbounds.getCenter());
// set zoom to fit all coord
map.fitBounds(latlngbounds);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM