-2

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

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

所以寻求帮助,谢谢!

<!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>
4

1 回答 1

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;
  }

地理编码器是异步的,返回时地址的值是未定义的。

改为在回调函数中使用返回的数据,如下所示(未测试):

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);
  });
}      
于 2012-12-06T14:56:51.093 回答