javascript - 计算多个纬度/经度的中心点

标签 javascript google-maps latitude-longitude

我正在使用 Google map 显示多个纬度/经度点。

但是 Google map 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(),

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

这怎么可能做到?

最佳答案

这有效。请参阅 Map.fitBounds 的文档和 LatLngBounds详情:

<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>

关于javascript - 计算多个纬度/经度的中心点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18288605/

相关文章:

Javascript 和 Gmail 相当于 Applescript 和 Apple Mail

javascript - 谷歌地图 V3 : Position a Clear X on input search

iphone - ios在 map 屏幕上获取东北和西南坐标

MySQL - 使用 BETWEEN 搜索负经度值

java - 如何从图片中获取经纬度信息

javascript - 如何将 id 传递给 jquery 中的函数?

javascript - 如何将 Cheerio DOM 节点转回 html?

javascript - jasny bootstrap offcanvas 不会在手机上滚动

android - 谷歌地图 API 黑屏

java - 将经度/纬度转换为 X/Y 坐标