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