gpt4 book ai didi

javascript - 使用 JavaScript 定位

转载 作者:搜寻专家 更新时间:2023-11-01 04:27:40 25 4
gpt4 key购买 nike

我正在编写一个脚本来获取我可以用来使我的谷歌地图实例居中的地理位置(纬度、经度)。现在我使用 2 种可能的技术。一个是 google.loader.ClientLocation 对象。我还没有测试过这个,因为它为我返回 null。我想是因为我不住在固定地点(库拉索岛威廉斯塔德使用无线互联网连接。所以我的调制解调器是无线的。)。

因此,我使用 navigator.geolocation 制定了备份计划。这在 Chrome 中运行良好,但 firefox 会超时并且在 IE 中根本不起作用。

有没有人知道获取用户地理定位的好替代方法,或者有没有人对我的代码提出建议,如何让它变得更稳定。

我为 navigator.geolocation 设置了超时,因为我不希望我的用户等待超过 5 秒。增加超时不会提高 firefox 的可靠性。

function init_tracker_map() {
var latitude;
var longitude;

if(google.loader.ClientLocation) {
latitude = (google.loader.ClientLocation.latitude);
longitude = (google.loader.ClientLocation.longitude);
buildMap(latitude, longitude);
}
else if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function(position) {
latitude = (position.coords.latitude);
longitude = (position.coords.longitude);
buildMap(latitude, longitude);
},
function errorCallback(error) {
useDefaultLatLon();
},
{
enableHighAccuracy:false,
maximumAge:Infinity,
timeout:5000
}
);
}
else {
useDefaultLatLon();
}
}

function useDefaultLatLon() {
latitude = (51.81540697949437);
longitude = (5.72113037109375);
buildMap(latitude, longitude);
}

附言。我知道在 SO 上还有更多类似的问题,但找不到明确的答案。我希望人们有一些新发现。

更新:也试过谷歌齿轮。再次在 chrome 中成功。在 FF 和 IE 中失败。

var geo = google.gears.factory.create('beta.geolocation');
if(geo) {
function updatePosition(position) {
alert('Current lat/lon is: ' + position.latitude + ',' + position.longitude);
}

function handleError(positionError) {
alert('Attempt to get location failed: ' + positionError.message);
}
geo.getCurrentPosition(updatePosition, handleError);
}

更新 2: navigator.geolocation 在我工作地点的 FF 中工作正常。

最终结果

这很好用。从 ipinfodb.org 获取 api key

var Geolocation = new geolocate(false, true);
Geolocation.checkcookie(function() {
alert('Visitor latitude code : ' + Geolocation.getField('Latitude'));
alert('Visitor Longitude code : ' + Geolocation.getField('Longitude'));
});

function geolocate(timezone, cityPrecision) {
alert("Using IPInfoDB");
var key = 'your api code';
var api = (cityPrecision) ? "ip_query.php" : "ip_query_country.php";
var domain = 'api.ipinfodb.com';
var version = 'v2';
var url = "http://" + domain + "/" + version + "/" + api + "?key=" + key + "&output=json" + ((timezone) ? "&timezone=true" : "&timezone=false" ) + "&callback=?";
var geodata;
var JSON = JSON || {};
var callback = function() {
alert("lol");
}

// implement JSON.stringify serialization
JSON.stringify = JSON.stringify || function (obj) {
var t = typeof (obj);
if (t != "object" || obj === null) {
// simple data type
if (t == "string") obj = '"'+obj+'"';
return String(obj);
}
else {
// recurse array or object
var n, v, json = [], arr = (obj && obj.constructor == Array);
for (n in obj) {
v = obj[n]; t = typeof(v);
if (t == "string") v = '"'+v+'"';
else if (t == "object" && v !== null) v = JSON.stringify(v);
json.push((arr ? "" : '"' + n + '":') + String(v));
}
return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
}
};

// implement JSON.parse de-serialization
JSON.parse = JSON.parse || function (str) {
if (str === "") str = '""';
eval("var p=" + str + ";");
return p;
};

// Check if cookie already exist. If not, query IPInfoDB
this.checkcookie = function(callback) {
geolocationCookie = getCookie('geolocation');
if (!geolocationCookie) {
getGeolocation(callback);
}
else {
geodata = JSON.parse(geolocationCookie);
callback();
}
}

// Return a geolocation field
this.getField = function(field) {
try {
return geodata[field];
} catch(err) {}
}

// Request to IPInfoDB
function getGeolocation(callback) {
try {
$.getJSON(url,
function(data){
if (data['Status'] == 'OK') {
geodata = data;
JSONString = JSON.stringify(geodata);
setCookie('geolocation', JSONString, 365);
callback();
}
});
} catch(err) {}
}

// Set the cookie
function setCookie(c_name, value, expire) {
var exdate=new Date();
exdate.setDate(exdate.getDate()+expire);
document.cookie = c_name+ "=" +escape(value) + ((expire==null) ? "" : ";expires="+exdate.toGMTString());
}

// Get the cookie content
function getCookie(c_name) {
if (document.cookie.length > 0 ) {
c_start=document.cookie.indexOf(c_name + "=");
if (c_start != -1){
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end == -1) {
c_end=document.cookie.length;
}
return unescape(document.cookie.substring(c_start,c_end));
}
}
return '';
}
}

最佳答案

使用 javascript 的地理定位将适用于 HTML 5 兼容的浏览器,因此完全排除了 IE。

您的替代方法是使用 IP 地址来确定大致的纬度/经度。

使用这种替代方法并假设您找到了具有准确且完整的纬度/经度到 IP 映射的提供商,您将只能获得 ISP 的纬度/经度(或 ISP 连接到互联网的最近点) ).

希望这会重置您的期望(关于位置准确性)

关于javascript - 使用 JavaScript 定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4405061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com