-1
我有谷歌地理位置API問題。它正在返回我目前位置的經度緯度&。當我第一次集成時,它返回正確,但現在返回錯誤。請看看我的下面的JavaScript代碼。谷歌地理定位API問題
$(document).ready(function(){
var apiGeolocationSuccess = function (position) {
alert("API geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var tryAPIGeolocation = function() {
jQuery.post("https://www.googleapis.com/geolocation/v1/geolocate?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx", function (success) {
apiGeolocationSuccess({coords: {latitude: success.location.lat, longitude: success.location.lng}});
})
.fail(function (err) {
alert("API Geolocation error! \n\n" + err);
});
};
var browserGeolocationSuccess = function (position) {
//alert("Browser geolocation success!\n\nlat = " + position.coords.latitude + "\nlng = " + position.coords.longitude);
};
var browserGeolocationFail = function (error) {
switch (error.code) {
case error.TIMEOUT:
//alert("Browser geolocation error !\n\nTimeout.");
if (error.TIMEOUT)
{
tryAPIGeolocation();
}
break;
case error.PERMISSION_DENIED:
if (error.message.indexOf("Only secure origins are allowed") == 0) {
tryAPIGeolocation();
}
break;
case error.POSITION_UNAVAILABLE:
alert("Browser geolocation error !\n\nPosition unavailable.");
break;
}
};
var tryGeolocation = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
browserGeolocationSuccess,
browserGeolocationFail,
{maximumAge: 50000, timeout: 20000, enableHighAccuracy: true});
}
};
tryAPIGeolocation();
如果我運行上面的代碼我得到這個值(緯度:23.0401214)(經度:72.5163579),但這是錯誤的。它返回ahemdabad城市lat &長。
正確的值應該是:22.3039,70.8022
,請與我的問題,一起來看看。
是的,但html 5地理定位服務要求https安全域不能用於http只。 –
這是正確的。您需要使用https –
好的,除了html5技術以外,沒有其他方法可以獲得用戶的確切位置嗎? –