Ich arbeite an einem Skript, um die Geolocations (Lat, Lon), die ich verwenden kann, um meine Instanz von Google Maps zu zentrieren. Für jetzt arbeite ich mit 2 möglichen Technologien. Einer ist das Objekt google.loader.ClientLocation
. Ich habe das noch nicht getestet, weil es für mich null zurückgibt. Ich denke, weil ich nicht an einem normalen Ort lebe (Willemstad, Curacao mit einer drahtlosen Internetverbindung. Also mein Modem ist drahtlos.).Geolocations mit Javascript
Deshalb habe ich einen Backup-Plan mit navigator.geolocation
gemacht. Das funktioniert gut in Chrome, aber Firefox gibt eine Zeitüberschreitung und es funktioniert überhaupt nicht in IE.
Kennt jemand eine gute alternative Methode, um die geolocation des Benutzers zu erhalten, oder hat jemand Empfehlung zu meinem Code, wie es stabiler werden kann.
Ich habe eine Zeitüberschreitung für navigator.geolocation
, weil ich nicht möchte, dass meine Benutzer auf mehr als 5 Sekunden warten. Das Erhöhen der Zeitüberschreitung verbessert die Zuverlässigkeit von Firefox nicht.
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);
}
ps. Ich bin mir bewusst, dass es auf SO mehr Fragen wie diese gibt, aber ich konnte keine klare Antwort finden. Ich hoffe, dass die Leute neue Entdeckungen machen.
Update: Versuchte google Zahnräder auch. Erfolgreich in Chrom wieder. Fails in FF und 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);
}
Update 2:navigator.geolocation
arbeitet in FF von meiner Arbeit Lage in Ordnung.
Endergebnis
Dies funktioniert gut. Holen Sie sich einen API-Schlüssel von ipinfodb.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 '';
}
}
Danke für den Link. Einige Empfehlungen mögen dort nützlich sein, aber sie konzentrieren sich stark auf mobile Geräte. –
Ist der Code nicht so konzipiert, dass er zwischen Mobilgerät und Web übertragen werden kann (insbesondere unter der W3C-API-Spezifikation)? Verständlicherweise gibt es das GPS nicht, aber ich dachte, dass es sonst auf etwas Sicheres zurückfallen sollte. – jocull