谷歌地理编码迟到(仍然落后一步)

时间:2012-12-06 14:22:23

标签: google-maps google-maps-api-3 google-maps-markers google-geocoder

我把我的整个代码放在那里。试图确定位置地址。我的脚本应该创建一个带有地理编码地址的标记(在控制台 - 变量kktina中)。

但不幸的是,并非一切都好。因为脚本仍然解析以前的位置(或者它需要先前的坐标,dunno)。也许代码中有错误,但我不能再这样做了..

所以寻求帮助,谢谢!

<!DOCTYPE html>
<html>
<head>
  <title>Google Maps JavaScript API v3 Example: Map Simple</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  <meta charset="utf-8">
  <style>
  html, body, #map_canvas {
    margin: 0;
    padding: 0;
    height: 100%;
  }
  </style>
  <link rel="stylesheet" type="text/css" href="style.css">
  <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places,geometry"></script>
  <script src="http://code.jquery.com/jquery-1.7.min.js" type="text/javascript"></script>
  <script>
  var map,marker;
  var geocoder = new google.maps.Geocoder();
  var markers = [];
  var img,icon,type;
  var prenos,pos,address = "5",point;


      function clearMarkersFromArray() {
    if (markers.length>0){
      markers[0].setMap(null);
      markers.shift(markers[0]);
    }
  }

  function geocodePosition(pos) {
    geocoder.geocode({
      latLng: pos
    }, function(responses) {
      if (responses && responses.length > 0) {
        address = responses[0].formatted_address;
      } else {
        address = 'Cannot determine address at this location.';
      }
    });
    return address;
  }

  function initialize() {

    var mapOptions = {
      zoom: 7,
      center: new google.maps.LatLng(49,19),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions),

    google.maps.event.addListener(map, 'click', function(response) {
      clearMarkersFromArray();
      point = new google.maps.LatLng(response.latLng.$a,response.latLng.ab); 
      var kktina = geocodePosition(point);
      console.log(kktina);
      var marker = new google.maps.Marker({
        position: point,
        draggable: true
      });
      markers.push(marker)
      marker.setMap(map);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
  <div id="map_canvas" style="height:400px;width:700px;"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

你不能这样做:

  function geocodePosition(pos) {
    geocoder.geocode({
      latLng: pos
    }, function(responses) {
      if (responses && responses.length > 0) {
        address = responses[0].formatted_address;
      } else {
        address = 'Cannot determine address at this location.';
      }
    });
    return address;
  }

Geocoder是异步的,返回时地址的值是未定义的。

使用回调函数中返回的数据,类似这样(未经测试):

var kktina = null;
function geocodePosition(pos) {
  geocoder.geocode({
    latLng: pos
  }, function(responses) {
    if (responses && responses.length > 0) {
      kktina = responses[0].formatted_address;
    } else {
      kktina = 'Cannot determine address at this location.';
    }
   console.log(kktina);
    var marker = new google.maps.Marker({
      position: point,
      draggable: true
    });
    markers.push(marker)
    marker.setMap(map);
  });
}