Javascript中的反向地理编码返回MissingKeyMap错误

时间:2016-11-04 06:55:59

标签: javascript google-maps geolocation geocoding reverse-geocoding

我正在尝试访问访问我网页的客户的格式化地址。

为此我编写了函数来获取lat long,然后将其反转地理编码为格式化地址。

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
 function coordinates_to_address(lat, lng) {
    var latlng = new google.maps.LatLng(lat, lng);
    var geocoder= new google.maps.Geocoder();
    geocoder.geocode({'latLng': latlng}, function(results, status) {
        if(status == google.maps.GeocoderStatus.OK) {
            if(results[0]) {
                $('#address_current').text(results[0].formatted_address);
            } else {
                alert('No results found');
            }
        } else {
            var error = {
                'ZERO_RESULTS': 'No address'
            }

            // alert('Geocoder failed due to: ' + status);
            $('#address_new').html('<span class="color-red">' + error[status] + '</span>');
        }
    });
}

navigator.geolocation.getCurrentPosition(function(location) {
  console.log(location.coords.latitude);
  console.log(location.coords.longitude);
  console.log(location.coords.accuracy);
  var lat= location.coords.latitude;
  var long= location.coords.longitude
  coordinates_to_address(lat, long);
});

</script>

日志显示输出lat long

    10.8888888888888888
    76.43923530000001
    40

但是这个函数返回了一个错误:

  Google Maps API error: MissingKeyMapError https://developers.google.com/maps/documentation/javascript/error-messages#missing-key-map-error
  http://maps.google.com/maps/api/js?sensor=false
  Line 34

有没有更简单的方法来获取lat long的格式化地址?

1 个答案:

答案 0 :(得分:1)