2013-08-16 55 views
1

我有一个地图,我设置了一个中心 - 当我要求地图返回中心时,它返回相同的值。然而,地图正在放大我目前唯一的引脚。谷歌地图没有平移到我的地图中心

我希望地图缩小到相同的地方,不管针脚在哪里。

上下文:地图列出国家/地区的位置。目前只有一个,但我想让地图缩小以显示整个国家。

回答

2

您无法在Google地图的setCenter中指定国家/地区,因为它始终关注特定的坐标。 如果您希望缩放以国家为中心,则您将拥有国家边界(作为坐标)的数据库,并与fitBounds和LatLngBounds一起使用。

这里是哪里使用应latLngList从一个国家被加载的坐标范围的样本代码

var LatLngList = new Array (new google.maps.LatLng (52.537,-2.061), new google.maps.LatLng (52.564,-2.017)); 
// Create a new viewpoint bound 
var bounds = new google.maps.LatLngBounds(); 
// Go through each... 
for (var i = 0, LtLgLen = LatLngList.length; i < LtLgLen; i++) { 
    // And increase the bounds to take this point 
    bounds.extend (LatLngList[i]); 
} 
// Fit these bounds to the map 
map.fitBounds (bounds); 

参考:Zoom to Fit all markers

1

由于您没有共享任何代码,我猜你是做这样的事情。

var bounds = new google.maps.LatLngBounds(); 
bounds.extend (LATLNG); 
map.fitBounds(bounds); 

这会将所有点添加到边界并确保缩放级别满足所有点。在你的情况下,它会放大你的唯一点。

如果你想放大一个国家,但没有坐标,你可以通过使用地理编码获得它们;

var address = "India"; // THE COUNTRY/ADDRESS WILL COME HERE 
var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     map.fitBounds(results[0].geometry.viewport);results[0].geometry.location,map: map,icon:getGoogleIcon('purple')}); 
     } else { 
     alert(address + " not found"); 
     } 
    }); 

它会将边界设置为您提供的地址。