繁体   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