所以我使用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
Run Code Online (Sandbox Code Playgroud)
基本上这里发生的是我们得到当前位置,如果得到的话,两个"水印"被放置在两个字段中,表示"当前位置",两个隐藏字段用lat-long数据作为它们的值(它们被删除)在一开始所以他们不会每次都重复).还有两个按钮,它们具有与之相关的点击功能,可以执行相同的操作.不幸的是,每三次左右,它都有效.这有什么问题???
我正在寻找一个javascript函数或jquery库来将地理位置代码(例如42.2342,32.23452)转换为街道地址
举些例子.
navigator.geolocation.getCurrentPosition(
function(pos) {
$("#lat_field").val(pos.coords.latitude);
$("#long_field").val(pos.coords.longitude);
}
);
Run Code Online (Sandbox Code Playgroud)
这是一个获取地址数据的google api网址
http://maps.googleapis.com/maps/api/geocode/json?latlng=41.03531125,29.0124264&sensor=false
我想看看"formatted_address":"HacıHesnaHatun Mh.,PaşaLimanıCd2-26,34674 Istanbul,Türkiye",
navigator.geolocation.getCurrentPosition(
function(pos) {
$("#lat_field").val(pos.coords.latitude);
$("#long_field").val(pos.coords.longitude);
$("#adress_data").getaddrfromlatlong(pos.coords.latitude,pos.coords.longitude)
}
);
Run Code Online (Sandbox Code Playgroud)
这个功能应该如何?``getaddrfromlatlong()