I have been trying to use Google Maps API in one of my projects and the problem is it's not showing me the current exact location even after allowing the location access (it's a Website Project). I mean it's outputting the location but like 200 metres away. When I try to use Google Maps' web version on the same device it's showing me the current location +-20 metres. We are trying to find the exact longitude and latitude. Here is the JavaScript
function getCurrentLocation() {
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
}
}
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var lat_and_long = pos.lat+","+pos.lng;
$("#geoplete").geoplete("find", lat_and_long);
}
}
$("#geoplete").bind("geocode:dragged", function(event, latLng){
$("input[name=lat]").val(pos.lat());
$("input[name=lng]").val(pos.lng());
$("#reset").show();
console.log(latLng);
});
console.log(pos);
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
We are using the following Google API with jQuery Geocoding and Places Autoplete Plugin (1.7.0)
;key=AIzaSyDJ2bLO-XXXXXXX"
I have been trying to use Google Maps API in one of my projects and the problem is it's not showing me the current exact location even after allowing the location access (it's a Website Project). I mean it's outputting the location but like 200 metres away. When I try to use Google Maps' web version on the same device it's showing me the current location +-20 metres. We are trying to find the exact longitude and latitude. Here is the JavaScript
function getCurrentLocation() {
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
}
}
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var lat_and_long = pos.lat+","+pos.lng;
$("#geoplete").geoplete("find", lat_and_long);
}
}
$("#geoplete").bind("geocode:dragged", function(event, latLng){
$("input[name=lat]").val(pos.lat());
$("input[name=lng]").val(pos.lng());
$("#reset").show();
console.log(latLng);
});
console.log(pos);
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
We are using the following Google API with jQuery Geocoding and Places Autoplete Plugin (1.7.0)
https://maps.googleapis./maps/api/js?libraries=places&key=AIzaSyDJ2bLO-XXXXXXX"
- Google Maps (if you are logged in) has a more accurate position for you than can be acquired from HTML5 geolocation. – geocodezip Commented Aug 23, 2018 at 2:45
-
I don't think you can use the
geoplete
jQuery plugin to search for a location using coordinates, I think thefind
method only works with addresses. – Titus Commented Aug 23, 2018 at 2:53 - Hi thanks for the reply. I used a new phone and tried to search the exact location in incognito mode ( safari browser) through google maps its showing the exact location, But as I tried to use the get current location in my web project it again showed me 100-200 metres away. @geocodezip – jewelhuq Commented Aug 23, 2018 at 2:56
- Do you know any library or code that will give me close to exact loaction in the samw way I have been getting through using google maps website @Titus – jewelhuq Commented Aug 23, 2018 at 2:58
- Was the phone using HTML5 geolocation or GPS? (My suspicion is it is using HTML5 geolocation, which would have the same accuracy as a desktop using HTML5 geolocation) – geocodezip Commented Aug 23, 2018 at 2:59
2 Answers
Reset to default 6Google maps API gives locations based on below sources :
GPS: Gives you location by using satellites (up to around 20 meters) sometimes its inaccurate like bad weather conditions, underground or if you are you're inside buildings.
Wi-Fi: The location of Wi-Fi networks IP address.
Cell tower: Your connection to a mobile cellular network tower can be accurate up to a few thousand meters.
DESKTOP BROWSER : while browse through desktop browsers like Mozilla Firefox and Google Chrome location rely on local network information, signals such as your puter's IP address
.
your browsers will ask you to share your location, if you allow to share then information about nearby wireless access points and IP address
will share to Google Location Services to get an estimate of your location.
The accuracy of location will vary by location.
MOBILE BROWSER : Mobile browsers like Chrome,Safari,Opera use GPS sensors
rather than WiFi network.
now enableHighAccuracy
option attribute provides a hint that the application would like to receive the best possible results.
This MAY result in slower response times or increased power consumption.
according to W3C's Geolocation API doc (https://w3c.github.io/geolocation-api/#high-accuracy) No guarantee is given that the API returns the device's actual location
.
The Geolocation API defines a high-level interface to location information associated only with the device hosting the implementation, such as latitude and longitude. The API itself is agnostic of the underlying location information sources. Common sources of location information include Global Positioning System (GPS) and location inferred from network signals such as IP address, RFID, WiFi and Bluetooth MAC addresses, and GSM/CDMA cell IDs, as well as user input. No guarantee is given that the API returns the device's actual location.
There is no way to get the exact location. Each location method contains errors. GPS is the most accurate, but the error is still in several meters range.
In JavaScript, your best option is to use
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
// ...
}
}
The Coordinates.accuracy
read-only property is a strictly positive double representing the accuracy, with a 95% confidence level, of the Coordinates.latitude
and Coordinates.longitude
properties expressed in meters.
See details here.