I m working on a script to get the Geolocations (Lat, lon) that I can use to center my instance of google maps. For now i work with 2 possible technologies. One is the google.loader.ClientLocation
object. I haven t tested this one yet because it returns null for me. I think because I m not living on a regular location (Willemstad, Curacao using a wireless internet connection. So my modem is wireless.).
因此,我采用了<代码>navigator.geoposition的备份计划。 这在 Chrome、但火ox中发挥了巨大作用,它给人留下了时间,在伊埃岛上根本不工作。
是否有任何人知道一种更好的替代方法,使用户的地理位置更加稳定,或者有人对我的代码提出建议。
I set a timeout for navigator.geoposition
因为我不想让我的用户等待5秒。 增加停用时间并不能提高火ox的可靠性。
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);
}
页: 1 我知道,就《SO》而言,还有更多的问题,但无法找到明确的答案。 我希望人们会发现一些新发现。
Update: Tried google gears aswell. Succesfull in chrome again. Fails in FF and 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);
}
www.un.org/Depts/DGACM/index_french.htm 从我的工作地点在FF进行罚款。
http://www.ohchr.org。
这发挥了巨大作用。 摘自Pipinfodb.org
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 ;
}
}