Google地理编码已经晚了(还落后一步) [英] Google geocode is late (still one step behind)

查看:92
本文介绍了Google地理编码已经晚了(还落后一步)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I put there my whole code. Trying to determine location address. My script should create still one marker with geocoded address (in console - variable kktina).

但是,不幸的是,并不是一切都好。因为通过脚本仍然可以解析以前的位置(或者它需要先前的坐标,不需要)。也许在代码中有错误,但我无法做到这一点了。

But, unfortunately, there isn't everything OK. Because by script still resolves previous location (or it takes previous coordinates, dunno). Maybe there is an error in code, but I'm not able to do this anymore..

所以请求帮助,谢谢!

So asking for help, thank you!

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


推荐答案

你不能这样做:

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

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

the Geocoder is asynchronous, the value of address is undefined when it is returned.

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

Use the returned data inside the callback function instead, something like this (not tested):

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

这篇关于Google地理编码已经晚了(还落后一步)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆