谷歌地图api不再工作了

时间:2014-11-24 14:43:52

标签: javascript api map geolocation

这段代码一直运作到今天:

navigator.geolocation.getCurrentPosition(
    function (pos) {
        var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
        console.log('ok');

        geocoder = new google.maps.Geocoder();            
        geocoder.geocode({'latLng': latlng}, function (results, status) {
            console.log('callback');
            console.log(status);
        });
    },
    errorGetCurrentPositionCallback,
    {enableHighAccuracy: true, timeout: 10000, maximumAge: 600000});        

我可以在控制台中看到" ok"消息,但不是"回调"。根本没有错误,我使用的是配额的0%。有些东西已经改变,但在我的代码中却没有。不再访问console.log。你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您的功能会检查导航器是否可以找到GPS位置(这纯粹是设备/浏览器功能;不是Google服务)。 但是您的功能不会检查地理编码器的状态(这是Google服务)。

看看你的控制台是这样的:

navigator.geolocation.getCurrentPosition(
  function (pos) {
      var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
      console.log('Just before geocode is called');
      geocoder = new google.maps.Geocoder();            
      geocoder.geocode({'latLng': latlng}, function (results, status) {
          console.log('callback of geocoder called');
          if (status == google.maps.GeocoderStatus.OK) {
            console.log('Status is okay');
          }
          else {
            console.log('Status is not okay, the reason: ' + status);
          }
      });
  },
  errorGetCurrentPositionCallback,
  {enableHighAccuracy: true, timeout: 10000, maximumAge: 600000}
);

这是一个有效的例子

<script src="http://maps.googleapis.com/maps/api/js"></script>
<script type="text/javascript">
    var map;
    function initialize() {
      var latlng = new google.maps.LatLng(50.45, 4.45);    // set your own default location.  This gets called if the parameters are left blank.
      var myOptions = {
        zoom: 15,
        center: latlng
      };
      map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
    }
    function errorGetCurrentPositionCallback() {
      console.log('Your device cannot find your location');
    }
    // initiate Google Maps + request the position of the user.
    function page_loaded() {
      initialize();
      navigator.geolocation.getCurrentPosition(
        // success callback
        function (pos) {
          var latlng = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
          var accuracy = pos.coords.accuracy;
          map.setCenter(latlng);
          var marker = new google.maps.Marker({
            position: latlng, 
            map: map, 
            title: "You are here! (at least within a " + accuracy + " meter radius)"
          });
          console.log('Just before geocode is called');
          geocoder = new google.maps.Geocoder();            
          geocoder.geocode({'latLng': latlng}, function (results, status) {
            console.log('callback of geocoder called');
            if (status == google.maps.GeocoderStatus.OK) {
              console.log('Status is okay; you have the address of this location');
              // 
            }
            else {
              console.log('Status is not okay, the reason: ' + status);
            }
          });
        },
        errorGetCurrentPositionCallback,
        {enableHighAccuracy: true, timeout: 10000, maximumAge: 600000}
      );
    }
    google.maps.event.addDomListener(window, 'load', page_loaded);
</script>
<style>
#map-canvas {
  width: 500px;
  height: 400px;
}
</style>
<div id="map-canvas"></div>