不能使用google.maps.LatLng对象's late

Cannot use google.maps.LatLng object's lat

本文关键字:late 对象 LatLng google maps 不能      更新时间:2023-09-26

我有这段代码

<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        margin: 0;
        padding: 0;
        height: 100%;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
var map;
function initialize() {
  var mapOptions = {
    zoom: 8,
    center: new google.maps.LatLng(-34.397, 150.644),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
      mapOptions);
  var polygonCoords = [
    new google.maps.LatLng(25.774252, -80.190262),
    new google.maps.LatLng(18.466465, -66.118292),
    new google.maps.LatLng(32.321384, -64.757370),
    new google.maps.LatLng(25.774252, -80.190262)
  ];
  var north = new google.maps.LatLng(), west = new google.maps.LatLng(), east = new google.maps.LatLng(), south = new google.maps.LatLng();
  north = polygonCoords[0];
  west = polygonCoords[0];
  east = polygonCoords[0];
  south = polygonCoords[0];
  for (i = 1; i < polygonCoords.length; i++) {
    if (north.lat < polygonCoords[i].lat) {
        north = polygonCoords[i];
    }
    if (south.lat > polygonCoords[i].lat) {
         south = polygonCoords[i];
    }
    if (west.lng > polygonCoords[i].lng) {
        west = polygonCoords[i];
    }
    if (east.lng < polygonCoords[i].lng) {
        east = polygonCoords[i];
    }
  }
    var center = new google.maps.LatLng(0,0);
    center.lat = (north.lat + south.lat) / 2;
    center.lng = (west.lng + east.lng) / 2;
    var num = (north.lat + south.lat) / 2;
    console.log(num);
}
google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

为什么num的结果是NaN ?我怎么用lat, lng, at number?这里可能的重复,我试图使用第一个解决方案,因为第二个解决方案不适合我。现在我被这个问题困住了。

google.maps.LatLng对象没有记录的lat属性(或lng属性)。它有一个。lat()方法返回它的纬度,一个。lng()方法返回它的经度。

var num = (north.lat() + south.lat())/2;

代码片段:

var map;
function initialize() {
  var mapOptions = {
    zoom: 5,
    center: new google.maps.LatLng(-34.397, 150.644),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);
  var polygonCoords = [
    new google.maps.LatLng(25.774252, -80.190262),
    new google.maps.LatLng(18.466465, -66.118292),
    new google.maps.LatLng(32.321384, -64.757370),
    new google.maps.LatLng(25.774252, -80.190262)
  ];
  var north = new google.maps.LatLng(),
    west = new google.maps.LatLng(),
    east = new google.maps.LatLng(),
    south = new google.maps.LatLng();
  north = polygonCoords[0];
  west = polygonCoords[0];
  east = polygonCoords[0];
  south = polygonCoords[0];
  for (i = 1; i < polygonCoords.length; i++) {
    if (north.lat() < polygonCoords[i].lat()) {
      north = polygonCoords[i];
    }
    if (south.lat() > polygonCoords[i].lat()) {
      south = polygonCoords[i];
    }
    if (west.lng() > polygonCoords[i].lng()) {
      west = polygonCoords[i];
    }
    if (east.lng() < polygonCoords[i].lng()) {
      east = polygonCoords[i];
    }
  }
  var center = {};
  center.lat = (north.lat() + south.lat()) / 2;
  center.lng = (west.lng() + east.lng()) / 2;
  var num = (north.lat() + south.lat()) / 2;
  console.log(num);
  var polygon = new google.maps.Polygon({
    path: polygonCoords,
    map: map
  })
  map.setCenter(center);
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
  margin: 0;
  padding: 0;
  height: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<div id="map-canvas"></div>