簡體   English   中英

防止在Geocoder Google Maps API內部循環

[英]Prevent Looping Inside Geocoder Google Maps API

我正在使用Google Map API。 我想給定的地址很長一段時間。 我做了這個

var geocoder = new google.maps.Geocoder();
    geocoder.geocode({ 'address': 'Stockholm' }, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var latitude = results[0].geometry.location.nb;
            var longitude = results[0].geometry.location.ob;
            console.log(latitude);
        }
});

但是我得到該控制台日志反復運行。 我只想讓它運行一次,但是我沒有發現任何想法。

聲明一個新變量,以顯示是否已調用地理編碼,並在首次發生時將其更改為true。

var geoCalled = false;

var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': 'Stockholm' }, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    var latitude = results[0].geometry.location.nb;
    var longitude = results[0].geometry.location.ob;
    if (!geoCalled) {
      console.log(latitude);
      geoCalled = true;
    }
  }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM