猿问

geoloc.getCurrentPosition有时不起作用

geoloc.getCurrentPosition有时不起作用

因此,我有一个非常简单的JS使用导航器.geolocation.getCurrentPoitationjammy。

$(document).ready(function(){
  $("#business-locate, #people-locate").click(function() {
    navigator.geolocation.getCurrentPosition(foundLocation, noLocation);
  });

  navigator.geolocation.getCurrentPosition(foundLocation, noLocation);

  function foundLocation(position) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
    var userLocation = lat + ', ' + lon;
    $("#business-current-location, #people-current-location").remove();
    $("#Near-Me")
      .watermark("Current Location")
      .after("<input type='hidden' name='business-current-location' id='business-current-location' value='"+userLocation+"' />");
    $("#people-Near-Me")
      .watermark("Current Location")
      .after("<input type='hidden' name='people-current-location' id='people-current-location' value='"+userLocation+"' />");
  }
  function noLocation() {
    $("#Near-Me").watermark("Could not find location");
    $("#people-Near-Me").watermark("Could not find location");
  }})//end DocReady

基本上,我们得到的是当前位置,如果得到了,两个“水印”放置在两个字段中,上面写着“当前位置”,两个隐藏字段是以长数据作为值创建的(它们在开始时被删除,这样它们就不会每次都被复制)。还有两个按钮有一个点击函数绑定到它们来做同样的事情。不幸的是,每隔三次左右,它就起作用了。这里有什么问题?


胡说叔叔
浏览 747回答 3
3回答

慕的地6264312

这是我要解决的问题,至少在当前的所有浏览器中都能工作(在Windows上,我没有Mac):if&nbsp;(navigator.geolocation)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;location_timeout&nbsp;=&nbsp;setTimeout("geolocFail()",&nbsp;10000); &nbsp;&nbsp;&nbsp;&nbsp;navigator.geolocation.getCurrentPosition(function(position)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clearTimeout(location_timeout); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;lat&nbsp;=&nbsp;position.coords.latitude; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;lng&nbsp;=&nbsp;position.coords.longitude; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;geocodeLatLng(lat,&nbsp;lng); &nbsp;&nbsp;&nbsp;&nbsp;},&nbsp;function(error)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clearTimeout(location_timeout); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;geolocFail(); &nbsp;&nbsp;&nbsp;&nbsp;});}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Fallback&nbsp;for&nbsp;no&nbsp;geolocation &nbsp;&nbsp;&nbsp;&nbsp;geolocFail();}如果有人在Firefox上单击“关闭”或选择“NO”或“从不共享”选项,这也会有效。笨重但有效。

翻过高山走不出你

我每次都这样:navigator.geolocation.getCurrentPosition(getCoor,&nbsp;errorCoor,&nbsp;{maximumAge:60000,&nbsp;timeout:5000,&nbsp;enableHighAccuracy:true});虽然不是很准确。有趣的是,在同一台设备上,如果我运行这台设备,它会把我推到100米左右(每次),但如果我去谷歌地图,它就能准确地找到我的位置。因此,尽管我认为enableHighAccuracy:true有助于它持续工作,但它似乎并不能使它更加准确.
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答