首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何阻止google地图地理编码在N秒后返回

如何阻止google地图地理编码在N秒后返回
EN

Stack Overflow用户
提问于 2015-08-23 03:42:05
回答 2查看 305关注 0票数 0

我正在调用google.maps.geocoder()来获取一些坐标。我想设置一个计时器,在3秒后停止地理编码调用,但我不确定该怎么做?

这是我的代码。

代码语言:javascript
复制
// here I want to stop any calls coming back from .geocode if a 
//timer times out after say 3-5 seconds.
var navbarGeocoder = new google.maps.Geocoder();
navbarGeocoder.geocode({ // call to geocode
  'address': navbarInput.value
}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {

    $("#latitude").val(results[0].geometry.location.G);
    $("#longitude").val(results[0].geometry.location.K);
    $("#mapType").val(results[0].types[0]);
  }
  $('form')[0].submit();
});

EN

回答 2

Stack Overflow用户

发布于 2015-08-23 04:07:16

它看起来不像是Geocoder()对象公开了一个超时方法。您可以使用jquery ajax调用其URL直接调用其地理编码服务:https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=API_KEY

从ajax调用服务,这将使您能够更好地控制通信。请参阅http://api.jquery.com/jquery.ajax/

票数 1
EN

Stack Overflow用户

发布于 2015-08-23 05:24:23

如果地理编码器在3秒内没有返回结果(通过或失败),那么一定是出了问题。只有在地理编码成功时才提交表单,否则不要提交表单,否则可能会让用户知道出了问题。

代码语言:javascript
复制
// here I want to stop any calls coming back from .geocode if a 
//timer times out after say 3-5 seconds.
var navbarGeocoder = new google.maps.Geocoder();
navbarGeocoder.geocode({ // call to geocode
  'address': navbarInput.value
}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {

    $("#latitude").val(results[0].geometry.location.G);
    $("#longitude").val(results[0].geometry.location.K);
    $("#mapType").val(results[0].types[0]);
    // only submit the form it the geocoder succeeded
    $('form')[0].submit();
  } else {
    // let the user know the request failed
    alert("geocoder failed, status = "+status);
  }

});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32160097

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档