[英]google geolocation api issue
我在使用Google地理位置api時遇到問題。 它返回了我當前位置的錯誤緯度和經度。 當我第一次集成時,它返回正確,但現在返回錯誤。 請看看我下面的javascript代碼。
$(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 city lat&long。
正確的值應該是:22.3039,70.8022
請看看我的問題。
瀏覽器提供的HTML 5地理位置可以使用GPS設備(高精度)或蜂窩塔式三角測量(中等精度)來檢測客戶端地理位置。
通過查看代碼,您的應用程序嘗試盡早檢測用戶地理位置,這很可能使用IP地址。
IP地址到位置的映射可能不准確,具體取決於您的用戶Internet服務提供商。
可以為用戶設備分配IP地址,該IP地址的地理位置注冊與用戶的實際地理位置非常不同。
因此,這是預期的行為。 在用戶同意共享您的地理位置之前,您可以盡力而為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.