Why is JavaScript geolocation only Approximate? - javascript

No matter how I use the JavaScript:
position.coords.latitude;
position.coords.longitude;
The returned coordinates are always at least a few city blocks off.
So - it will show the "start point" or "Your location" as a few streets over?
This approximate result seems new, as the code I used before produced a more accurate result.
I have tested on all browser's from a "https" web page.
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + '/' + position.coords.longitude;
}
</script>
Has something changed?
Why is the produced coordinates only approximate?

This is a tradeoff between speed and accuracy. Gelocation.getCurrentPosition() at MDN details the optional parameter enableHighAccuracy

JavaScript geolocation is only an approximation of the device's actual location because it relies on the device's built-in GPS, Wi-Fi, IP address lookup and/or network triangulation to determine your location. Additionally, the Geolocation API provides an estimated location, not an exact location. The accuracy of the location information can vary greatly and can be influenced by various factors, including the type of device, its settings, and the environment (buildings, trees, etc) in which it is used.
I used your function and indeed the latitude and longitude coordinates were not exact, there is a difference of a few meters (more than 30-40).

Related

Google Chrome: Override geolocation via console

So a little background for my question; I am writing a simple driving instructions web application using Google Maps directions API which provides me with a LatLng path along with text instructions.
In order to test this application (without driving around in a car) I need to simulate a geolocation-path. Google Chrome supports overriding geolocation data via the sensors developer settings, which works fine with one coordinate at the time.
So my question is - is it possible to set the browsers navigator.geolocation data via the console (i.e. javascript api) instead manually updating the value in the sensors settings menu?
I know that in this case I could just use another input source than the browser geolocation data and use a static array of LatLng's, or override the browsers navigator.geolocation.getCurrentPosition, but I figured that it would be more sophisticated to override the sensors instead.
Thanks in advance.
One way is to override the function navigator.geolocation.getCurrentPosition with your own custom function. Inside the custom function you can customize the value of latitude & longitude.
var customPosition = {};
customPosition.coords = {};
customPosition.coords.latitude = 41.89;
customPosition.coords.longitude = 2.89;
navigator.geolocation.getCurrentPosition = function(success, error){
success(customPosition);
};
function success(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
console.log("latitude: " + latitude);
console.log("longitude: " + longitude);
}

How to make HTML5 Geolocation more accurate?

I've been playing with the HTML5 Geolocation options and noticed that the results can be a bit erratic and wondered if there was a way to get them more consistent.
I've been doing all of my tests on an iphone 5 and just using the standard getCurrentPosition code with enableHighAccuracy: true and enableHighAccuracy: false, but sometimes when I click the code to get my position on a Google map it is sometimes about 0.25-0.5 mile out - yet (and this is a biggie) if I go to the Maps app on the phone it moves around a bit and then gets my position bang on.
Is there something I can do to get the same level of accuracy?
Speaking of accuracy - what does the position.coords.accuracy command actually do or mean? I've had a map position bang on where I am yet it says something like 1200 and I've had it show the pin about 400 yards away from my location and I get 165
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,showError,{maximumAge:600000, timeout:5000, enableHighAccuracy: true});
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
function showPosition(position) {
var x = document.getElementById("demo");
var marker = new google.maps.Marker({position: {lat:position.coords.latitude, lng:position.coords.longitude}, map: map, title: 'X'});
x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude + "<br>Accuracy: " + position.coords.accuracy;
}
Apologies I'm new to posting in this forum.
position.coords.accuracy is a measurement of meters. If you were to draw a circle around the Lat/Lng returned as its center and "accuracy as its radius then the UA suggests that your "real" location is in that circle somewhere.
enableHighAccuracy = true may remove mobile phone towers and wi-fi distribution points from the heuristic GPS calculation but I too am disappointed at the difference between a Chrome GPS mapped to Google Map as opposed to the Maps App. Also indoor readings could be few and far between if you go the accuracy route.
Experimenting with some algorithms where don't rely on it unless the accuracy is <= the last one (or until Xms or Y>accuracy readings have been received) may help.

Is there a way to continue background processes even when device is locked?

I am currently developing app that is 2 part:
1st part is truck driving app that drivers take with them when making deliveries to read order info's and such (they are using tablets that run android)
2nd part is monitoring app for managers to monitor statuses of deliveries and such (they are using computers)
I want to implement GPS tracking now on both sides. I know I can get Lat and Len of drivers and wrap it into setInterval to get (for example) every 3 secs, like so:
setInterval(function(){
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
}, 3000);
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
My only issue here is will this process persist if drivers lock their phones or even minimize browser.
Over all, is this good solution?

Get latitude and longitude of user in PHP or JavaScript for use in PHP

I have an Android app which gets the user's location (latitude and longitude). The latitude and longitude is used to query a database via a web service.
I am in the process of developing a website which will be another gateway into the backend. In order to make the website useful I need to get the latitude and longitude of the user, as the results are sorted by distance (i.e. distance from user's location to location of returned data which is a bar/restaurant).
So how do I get the latitude and longitude of the user?
It is not possible via PHP directly although if you are creating a website you can use the javascript geolocation API to query the browser about what it knows about the users current location:
<script>
var x = document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}
</script>
I have simply copied the code example from w3 schools vertabrim.
Source:
http://www.w3schools.com/html/html5_geolocation.asp

Accessing location using Javascript

I run the code shown below from different places but this results in same latitude and longitude value at different places.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body onload="getLocation()">
<p id="demo"> </p>
<script>
var x= document.getElementById("demo");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.innerHTML= position.coords.latitude;
x.innerHTML= position.coords.longitude;
var lat=position.coords.latitude;
var lon=position.coords.longitude;
}
</script>
**strong text**</body>
</html>
Any help would be appreciated.
In short, the problem isn't with your code, but most likely with your device. I can tell you're using the demo code from the W3Schools article on the geolocation API. That code seems to work fine for me, at least as much as I would expect.
Geolocation isn't terribly precise in all cases. According to MDN, the method use to determine location will be the most accurate available for your device at that time. If you're testing on a desktop browser, the results might be no more accurate than to the nearest city, and in some cases even more inaccurate than that (My home in Michigan registers as the middle of New York state, for some weird reason). If you test on a 3g or 4g phone, you might get better results, down to the nearest broadcast area. If you test on a phone with GPS, you might get highly accurate data, where you'll notice a difference if you move even a few feet or so. I say 'might' for these cases, because location data is a huge privacy concern area, and there's a lot of things that might interfere with geolocation to protect a user's privacy.
If you're seeing the same result from relativly small moves, this would easily be the cause. If you've travelled a few hundred miles to run your code elsewhere, then I would have to admit that there's something

Categories