navigator.geolocation.getCurrentPosition 有时有效,有时不起作用
所以我有一个非常简单的JS使用navigator.geolocation.getCurrentPosition jammy。
$(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
基本上,这里发生的事情是,我们得到当前位置,如果获得,两个“水印”被放置在两个显示“当前位置”的字段中,两个隐藏字段是用纬度数据作为其值创建的(它们在开始时被删除,所以它们不会每次都重复)。还有两个按钮具有与它们绑定的单击功能,可以执行相同的操作。不幸的是,每隔三次左右,它就会起作用。这有什么问题???