Google Map API v3 - 设置边界和中心

本文介绍了如何在Google Maps API V3中设置地图的边界和中心,特别是在根据标记自动居中和缩放地图的方法。讨论了使用LatLngBounds对象扩展边界,并使用setCenter和getBoundsZoomLevel方法进行适配。同时提到了对于不同形状如多边形、圆形和矩形调整地图边界的方法。

本文翻译自:Google Map API v3 — set bounds and center

I've recently switched to Google Maps API V3. 我最近切换到Google Maps API V3。 I'm working of a simple example which plots markers from an array, however I do not know how to center and zoom automatically with respect to the markers. 我正在编写一个简单的例子来绘制数组中的标记,但是我不知道如何相对于标记自动居中和缩放。

I've searched the net high and low, including Google's own documentation, but have not found a clear answer. 我搜索了网络的高低,包括谷歌自己的文档,但没有找到一个明确的答案。 I know I could simply take an average of the co-ordinates, but how would I set the zoom accordingly? 我知道我可以简单地采用坐标的平均值,但是如何相应地设置缩放?

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]
    });
  }
}

#1楼

参考:https://stackoom.com/question/6X1d/Google-Map-API-v-设置边界和中心


#2楼

Got everything sorted - see the last few lines for code - ( bounds.extend(myLatLng); map.fitBounds(bounds); ) 得到了所有排序 - 请参阅代码的最后几行 - ( 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);
}

#3楼

Yes, simply declare your new bounds object. 是的,只需声明您的新边界对象。

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

Then for each marker, extend your bounds object: 然后为每个标记扩展您的边界对象:

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

API: google.maps.LatLngBounds API: google.maps.LatLngBounds


#4楼

Use below one, 使用下面一个,

map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));


#5楼

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


#6楼

The answers are perfect for adjust map boundaries for markers but if you like to expand Google Maps boundaries for shapes like polygons and circles, you can use following codes: 答案非常适合调整标记的地图边界,但如果您想为多边形和圆形等形状展开Goog​​le地图边界,则可以使用以下代码:

For Circles 对于圈子

bounds.union(circle.getBounds());

For Polygons 对于多边形

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

For Rectangles 对于矩形

bounds.union(overlay.getBounds());

For Polylines 对于折线

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));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值