计算多个Lat / Long的中心点

时间:2013-08-17 12:05:48

标签: javascript google-maps latitude-longitude

我正在使用Google地图显示多个纬度/经度。

但谷歌地图的Javascript代码有代码片段来跟踪可用纬度/经度的纬度/长度的中心。

<script type="text/javascript">
    var locations = JSON.parse('<?php echo json_encode($data);?>'); //alert (locations);
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: new google.maps.LatLng(),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {

        marker = new google.maps.Marker({
            position: new google.maps.LatLng(locations[i][2], locations[i][3]),
            map: map
        });

        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                infowindow.setContent(locations[i][1]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }
</script>

在上面的代码中

center: new google.maps.LatLng(),

需要一个纬度/长度对,它可以是多个纬度/经度的中心点。

怎么可能这样做?

2 个答案:

答案 0 :(得分:2)

这很有效。有关详细信息,请参阅Map.fitBoundsLatLngBounds的文档:

<script type="text/javascript">
   var locations =  JSON.parse('<?php echo json_encode($data);?>');//alert (locations);
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;
    var bounds = new google.maps.LatLngBounds();
     for (i = 0; i < locations.length; i++) {

      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][2],locations[i][3]),
        map: map
      });
      bounds.extend(marker.getPosition());

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][1]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
    map.fitBounds(bounds);
  </script>

代码段

var locations = JSON.parse(
  JSON.stringify([
    ['AAA', 'BBB', 42, -72],
    ['BBB', 'CCC', 42.1, -72.2]
  ])); //alert (locations);
var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 10,
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();

var marker, i;
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {

  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][2], locations[i][3]),
    map: map
  });
  bounds.extend(marker.getPosition());

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][1]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}
map.fitBounds(bounds);
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>

答案 1 :(得分:1)

您应该使用map.getBounds()函数获取已添加标记的边界。

它返回一个对象,您可以在该对象上获得中心bound.getCenter()。

请参阅Find center of multiple locations in Google Maps