2009-10-12 57 views
274

我最近切换到Google Maps API V3。我正在绘制一个简单示例,它可以绘制数组中的标记,但我不知道如何自动对标记进行居中和缩放。Google Map API v3 - 设置边界和中心

我搜索了网络的高低,包括谷歌自己的文档,但还没有找到明确的答案。我知道我可以简单地选取坐标的平均值,但是如何设定相应的缩放比例?

function initialize() { 
    var myOptions = { 
    zoom: 10, 
    center: new google.maps.LatLng(-33.9, 151.2), 


    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions); 

    setMarkers(map, beaches); 
} 


var beaches = [ 
    ['Bondi Beach', -33.890542, 151.274856, 4], 
    ['Coogee Beach', -33.423036, 151.259052, 5], 
    ['Cronulla Beach', -34.028249, 121.157507, 3], 
    ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], 
    ['Maroubra Beach', -33.450198, 151.259302, 1] 
]; 

function setMarkers(map, locations) { 

    var image = new google.maps.MarkerImage('images/beachflag.png', 
     new google.maps.Size(20, 32), 
     new google.maps.Point(0,0), 
     new google.maps.Point(0, 32)); 
    var shadow = new google.maps.MarkerImage('images/beachflag_shadow.png', 

     new google.maps.Size(37, 32), 
     new google.maps.Point(0,0), 
     new google.maps.Point(0, 32)); 


     var lat = map.getCenter().lat(); 
     var lng = map.getCenter().lng();  


    var shape = { 
     coord: [1, 1, 1, 20, 18, 20, 18 , 1], 
     type: 'poly' 
    }; 
    for (var i = 0; i < locations.length; i++) { 
    var beach = locations[i]; 
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]); 
    var marker = new google.maps.Marker({ 
     position: myLatLng, 
     map: map, 
     shadow: shadow, 
     icon: image, 
     shape: shape, 
     title: beach[0], 
     zIndex: beach[3] 
    }); 
    } 
} 

回答

164

把一切都整理 - 看到最后几行代码 - (bounds.extend(myLatLng); map.fitBounds(bounds);

function initialize() { 
    var myOptions = { 
    zoom: 10, 
    center: new google.maps.LatLng(0, 0), 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    var map = new google.maps.Map(
    document.getElementById("map_canvas"), 
    myOptions); 
    setMarkers(map, beaches); 
} 

var beaches = [ 
    ['Bondi Beach', -33.890542, 151.274856, 4], 
    ['Coogee Beach', -33.923036, 161.259052, 5], 
    ['Cronulla Beach', -36.028249, 153.157507, 3], 
    ['Manly Beach', -31.80010128657071, 151.38747820854187, 2], 
    ['Maroubra Beach', -33.950198, 151.159302, 1] 
]; 

function setMarkers(map, locations) { 
    var image = new google.maps.MarkerImage('images/beachflag.png', 
    new google.maps.Size(20, 32), 
    new google.maps.Point(0,0), 
    new google.maps.Point(0, 32)); 
    var shadow = new google.maps.MarkerImage('images/beachflag_shadow.png', 
    new google.maps.Size(37, 32), 
    new google.maps.Point(0,0), 
    new google.maps.Point(0, 32)); 
    var shape = { 
    coord: [1, 1, 1, 20, 18, 20, 18 , 1], 
    type: 'poly' 
    }; 
    var bounds = new google.maps.LatLngBounds(); 
    for (var i = 0; i < locations.length; i++) { 
    var beach = locations[i]; 
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]); 
    var marker = new google.maps.Marker({ 
     position: myLatLng, 
     map: map, 
     shadow: shadow, 
     icon: image, 
     shape: shape, 
     title: beach[0], 
     zIndex: beach[3] 
    }); 
    bounds.extend(myLatLng); 
    } 
    map.fitBounds(bounds); 
} 
+6

谢谢!这些3.0文档对于这个功能的用途非常模糊。 – Bill 2010-08-30 15:06:48

+8

3.0文档出乎意料地模糊了很多事情发生的地方。 :( – Scottie 2010-11-29 12:15:01

+0

绝对完美,谢谢! – Rabbott 2011-01-17 21:45:23

382

是,简单地声明你的新边界对象。

var bounds = new google.maps.LatLngBounds(); 

然后,对于每个标记物,延伸您的边界对象:

bounds.extend(myLatLng); 
map.fitBounds(bounds); 

API:google.maps.LatLngBounds

+34

你也可以添加这个map.setCenter(bounds.getCenter()); – 2013-05-08 17:21:48

+0

拉曼的回答和评论都帮助我减少了我所需要的东西。 – JustJohn 2016-05-01 17:15:14

+0

@ Sam152:您可能对[500 rep相关问题的赏金]感兴趣(http://stackoverflow.com/questions/27094908/google-maps-keep-center-when-zooming)。 – 2017-03-28 00:49:17

-15

使用下一个,

map.setCenter(bounds.getCenter(),地图.getBoundsZoomLevel(边界));

+12

这是用于Google地图API第2版 – dave1010 2010-04-30 08:59:34

+0

您需要提供更深思熟虑的解决方案。我相信这是针对Google Maps v2的。 – AllJs 2017-02-16 20:54:05

-1

setCenter()方法仍适用于fitBounds()不存在的最新版Maps API for Flash。

5

我对谷歌地图API V3的建议是(不认为它可以更effeciently完成):

gmap : { 
    fitBounds: function(bounds, mapId) 
    { 
     //incoming: bounds - bounds object/array; mapid - map id if it was initialized in global variable before "var maps = [];" 
     if (bounds==null) return false; 
     maps[mapId].fitBounds(bounds); 
    } 
} 

在结果或数据U将适合在边界的所有点在地图窗口。

例完美的作品和u可以自由地点击此处查看www.zemelapis.lt

+0

如果'bounds'为'null',您可以'map [mapId] .getBounds()'而不是返回'false'。 – kaiser 2013-11-18 10:16:32

+0

@localtime实际上,您的网站需要Google Maps API密钥才能使用 – 2016-11-16 11:34:29

0

的答案是完美的调整地图边界标志,但如果你想扩大谷歌地图的边界像多边形和圆的形状,你可以使用以下代码:

对于圆

bounds.union(circle.getBounds()); 

多边形

polygon.getPaths().forEach(function(path, index) 
{ 
    var points = path.getArray(); 
    for(var p in points) bounds.extend(points[p]); 
}); 

对于矩形

bounds.union(overlay.getBounds()); 

折线

var path = polyline.getPath(); 

var slat, blat = path.getAt(0).lat(); 
var slng, blng = path.getAt(0).lng(); 

for(var i = 1; i < path.getLength(); i++) 
{ 
    var e = path.getAt(i); 
    slat = ((slat < e.lat()) ? slat : e.lat()); 
    blat = ((blat > e.lat()) ? blat : e.lat()); 
    slng = ((slng < e.lng()) ? slng : e.lng()); 
    blng = ((blng > e.lng()) ? blng : e.lng()); 
} 

bounds.extend(new google.maps.LatLng(slat, slng)); 
bounds.extend(new google.maps.LatLng(blat, blng)); 
相关问题