简体   繁体   中英

Find current location using Google Maps API in JavaScript

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;
        $("#geocomplete").geocomplete("find", lat_and_long);
    }
}

$("#geocomplete").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 Autocomplete Plugin (1.7.0) https://maps.googleapis.com/maps/api/js?libraries=places&key=AIzaSyDJ2bLO-XXXXXXX"

Google maps API gives locations based on below sources :

  1. 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.

  2. Wi-Fi : The location of Wi-Fi networks IP address.

  3. 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 computer'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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM