【问题标题】:fitbounds for google maps api v3 doesn't center on my polylinesgoogle maps api v3 的 fitbounds 不以我的折线为中心
【发布时间】:2012-01-25 03:52:19
【问题描述】:

我希望能够在我的网站的所有折线上进行居中缩放。

另外,我想为这些新坐标对中的每一个添加标记。我见过很多关于多个标记和多个多边形的例子,但从来没有关于折线的例子,我无法解决这个问题......

我看过一些非常有用的页面,例如:http://salman-w.blogspot.com/2009/03/zoom-to-fit-all-markers-polylines-or.html

但他们中的大多数人事先假设了某种知识,而我知道的不多。

这是我的代码:

<script type="text/javascript">

  function initialize() {
  var myLatLng = new google.maps.LatLng(45.397, 5.644);
  var centerLatLng = new google.maps.LatLng(35.71,-1.905);

  var myOptions = {
    zoom: 5,
    center: centerLatLng,
    scrollwheel: false,
    mapTypeControl: false,
    mapTypeId: google.maps.MapTypeId.SATELLITE
  };

  var map = new google.maps.Map(document.getElementById("map_canvas"),
      myOptions);

  var flightPlanCoordinates = [
    new google.maps.LatLng(45.397, 5.644),
    new google.maps.LatLng(28.713383,-17.905781),
    new google.maps.LatLng(20.713383,-19.905781),
    new google.maps.LatLng(25.713383,-29.905781),

  ];

  var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    strokeColor: "#dddd00",
    strokeOpacity: 1.0,
    strokeWeight: 3
  });

  flightPath.setMap(map);

}
</script>

在此先感谢,我对 googles API 和这个网站非常陌生。

【问题讨论】:

    标签: google-maps-api-3 marker polyline fitbounds google-polyline


    【解决方案1】:

    尝试将点的位置添加到边界对象,然后使用 fitBounds,如下所示:

    var bounds = new google.maps.LatLngBounds();
    
    var point1 = new google.maps.LatLng(45.397, 5.644);
    bounds.extend(point1);
    var point2 = new google.maps.LatLng(28.713383,-17.905781);
    bounds.extend(point2);
    var point3 = new google.maps.LatLng(20.713383,-19.905781);
    bounds.extend(point3);
    var point4 = new google.maps.LatLng(25.713383,-29.905781);
    bounds.extend(point4);
    
    map.fitBounds(bounds);
    

    【讨论】:

      猜你喜欢
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      • 2014-03-25
      • 1970-01-01
      • 2011-01-27
      • 2012-03-19
      相关资源
      最近更新 更多