我使用以下代码:
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/