Google 地理定位 JS API 胡说八道 Lat/Long

Google Geolocation JS API Giving Nonsense Lat/Long

我正在使用以下代码,使用有效的 API 键,从 Google Geocoder JS API:

获取纬度和经度
<script async defer type="text/javascript"
    src="http://maps.google.com/maps/api/js?key=[key]">
</script>
<script>
    var geocoder = new google.maps.Geocoder();
    var address = "1600 Amphitheatre Parkway, Mountain View, CA";
    geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK)
        {
            console.log (results[0]);
            // results[0].geometry.location.lat
            // results[0].geometry.location.lng
        }
        else console.log(status, results);
    });
</script>

对 Google 服务器的查询工作正常,并返回结果。问题是无论我输入什么地址,location.lat 返回为 _.E/this.lat()location.lng 返回为 _.E/this.lng()。视口坐标很好,但实际的经纬度结果对我来说是无稽之谈。如果我将代码放入函数并将其作为回调传递,也会发生同样的事情。

有没有人遇到过这种情况?有什么我想念的吗?当我搜索时,我在任何地方都找不到关于这个问题的任何信息,这是我第一次使用 API.

results[0].geometry.location 是一个 google.maps.LatLng。它没有 .lat/.lng 属性,它们是函数,你需要调用它们:

  var geocoder = new google.maps.Geocoder();
  var address = "1600 Amphitheatre Parkway, Mountain View, CA";
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      console.log(results[0]);
      var lat = results[0].geometry.location.lat();
      var lng = results[0].geometry.location.lng();
      map.setCenter(results[0].geometry.location);
    } else console.log(status, results);
  });

proof of concept fiddle

代码片段:

var geocoder;
var map;

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var geocoder = new google.maps.Geocoder();
  var address = "1600 Amphitheatre Parkway, Mountain View, CA";
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      console.log(results[0]);
      var lat = results[0].geometry.location.lat();
      var lng = results[0].geometry.location.lng();
      var iw = new google.maps.InfoWindow();
      iw.setContent("lat:" + lat + "<br>lng:" + lng);
      iw.setPosition(results[0].geometry.location);
      iw.open(map);
      map.setCenter(results[0].geometry.location);
    } else console.log(status, results);
  });
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>