简体   繁体   English

第二次点击后会出现Google地图反向地理编码地址吗?

[英]Google Maps reverse geocoding address appears after second click?

Currently if I click on a marker, error in console shows "address undefined" but if i click it again the address shows up, why is this happening? 目前,如果我点击一个标记,控制台中的错误显示“地址未定义”但如果我再次点击它地址显示,为什么会发生这种情况?

What my listener looks like: 我的听众看起来像什么:

map.data.addListener('click', function(event) {

      var lat = event.latLng.lat(); 
      var lng = event.latLng.lng(); 

      function getReverseGeocodingData() {
      var latlng = new google.maps.LatLng(lat, lng);
      // This is making the Geocode request
      var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'latLng': latlng }, function (results, status) {
            if (status !== google.maps.GeocoderStatus.OK) {
                alert(status);
            }
            // This is checking to see if the Geoeode Status is OK before proceeding
            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results[0].formatted_address);
                address = (results[0].formatted_address);
            }
        });
    }
      getReverseGeocodingData(lat, lng);

      infoWindow.setContent("Address: " + address + "<br>Vehicle: " + event.feature.getProperty('deviceID')+"<br> Speed: "+event.feature.getProperty('speedKPH'));
      infoWindow.setPosition(event.latLng);
      infoWindow.setOptions({pixelOffset: new google.maps.Size(0,-34)});
      infoWindow.open(map);

      });

thank you for your time and help in advance. 感谢您的时间和提前帮助。

geocoder.geocode works asynchronously, which means the callback will be invoked later ™. geocoder.geocode异步工作,这意味着稍后将调用回调™。 You are calling this method from the synchronous method getReverseGeocodingData , and then proceed to use the address data immediately afterwards. 您正在从同步方法getReverseGeocodingData调用此方法,然后立即继续使用地址数据。

This can't work. 这不行。

Asynchronous communication can be visualized with traditional paper mail. 可以使用传统的纸质邮件可视化异步通信 Imagine you send a letter to Google to get the address at x,y . 想象一下,您发送一封信给Google,以获取x,y的地址。 After you put the letter in the postbox, you don't have the result just yet, so you can't print that sign with the address on it yet. 将信件放入邮箱后,您还没有结果,因此您无法打印带有地址的标志。 But you can do other stuff, like repainting your house (yeah, the metaphor is stretched). 但你可以做其他的事情,比如重画你的房子(是的,这个比喻很紧张)。 You will have to be patient to wait for the answer via mail. 您必须耐心等待通过邮件回答。

A few days later the mailman rings, and delivers you the answer from Mountain View. 几天后,邮递员响起,并向您提供山景城的答案。 It says: " x,y is at Hauptstraße 22 ". 它说:“ x,yHauptstraße22 ”。 Now you can start printing that sign (and this is where the metaphor ends) to the status bar of your browser. 现在,您可以开始将该符号(这是隐喻结束的位置)打印到浏览器的状态栏。

On the other hand, you can visualize synchronous communication with phone calls. 另一方面,您可以使用电话可视化同步通信 You get the answer immediately, and you can't do anything else during the call. 您会立即得到答案,并且在通话期间您无法执行任何其他操作。 After you hung up, you got the answer. 挂断电话后,你得到了答案。

In JavaScript, we are pretty much stuck with the asynchronous model. 在JavaScript中,我们几乎坚持使用异步模型。 If this is good or bad is not for today to decide ;-) 如果这是好还是坏,今天不能决定;-)

So thanks thriqon i understood the problem, 所以,谢谢你,我理解了这个问题,

and have come up with this solution which i'm not sure how correct it is, but it does what i need it to do. 并提出了这个解决方案,我不确定它是多么正确,但它做了我需要它做的事情。 It calls for the address once they hover over the point in the background without popping up the infowindow and when they click, tada, the address is shown in the infowindow! 一旦它们悬停在背景中的点上而没有弹出信息窗,它会调用地址,当他们点击时,tada,地址显示在infowindow! hope this helps some people! 希望这有助于一些人! messsssy code 麻烦的代码

 map.data.addListener('mouseover', function(event) {

  var lat = event.latLng.lat(); 
  var lng = event.latLng.lng(); 

   function getReverseGeocodingData(lat, lng) {
  var latlng = new google.maps.LatLng(lat, lng);
  // This is making the Geocode request
  var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'latLng': latlng }, function (results, status) {
        if (status !== google.maps.GeocoderStatus.OK) {
            alert(status);
        }
        // This is checking to see if the Geoeode Status is OK before proceeding
        if (status == google.maps.GeocoderStatus.OK) {
            console.log(results[0].formatted_address);
           address = (results[0].formatted_address);
           return address;
        } 
    });
}
  getReverseGeocodingData(lat, lng);

  map.data.addListener('click', function(event) {

  infoWindow.setContent("Address: " + address + "<br>Vehicle: " + event.feature.getProperty('deviceID') +"<br> Speed: "+event.feature.getProperty('speedKPH')+"<br> Heading:"+event.feature.getProperty('heading'));
  infoWindow.setPosition(event.latLng);
  infoWindow.setOptions({pixelOffset: new google.maps.Size(0,-34)});
  infoWindow.open(map);

  });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM