169

I am developing a mobile based web-site, there I have integrated Google Maps, I need to fill the 'From' field of Google Maps dynamically.

Is it possible to get the GPS location from web browser and fill it up in the 'From' field of a Google Map dynamically?

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Ganesh
  • 1,707
  • 2
  • 11
  • 3

7 Answers7

203

If you use the Geolocation API, it would be as simple as using the following code.

navigator.geolocation.getCurrentPosition(function(location) {
  console.log(location.coords.latitude);
  console.log(location.coords.longitude);
  console.log(location.coords.accuracy);
});

You may also be able to use Google's Client Location API.

This issue has been discussed in Is it possible to detect a mobile browser's GPS location? and Get position data from mobile browser. You can find more information there.

AvidDabbler
  • 551
  • 6
  • 19
dochoffiday
  • 5,515
  • 6
  • 32
  • 41
  • Is the Google Client Location API mentioned still around? The link provided goes to Google Loader – Shane N Feb 24 '12 at 02:04
  • 1
    I don't see it on that page as one of the available APIs. - You may be able to use to Google Latitude API: http://code.google.com/apis/latitude/ - With this it is possible to get the client's current location: http://code.google.com/apis/latitude/v1/using_rest.html#RetrievingCurrent – dochoffiday Feb 24 '12 at 15:42
  • 2
    For future readers, [the accuracy is measured in meters](http://stackoverflow.com/q/6719542/2840103). – johnnyRose Dec 29 '15 at 19:23
  • Hyy can we enable the sharing location autmatically via coding – Mr. Tomar Apr 30 '16 at 05:35
  • 3
    Does this request the use of your location? – Jack Sep 17 '17 at 23:51
  • I believe it's good practive to wrap this around if (navigator.geolocation) {} https://developers.google.com/web/fundamentals/native-hardware/user-location#browser_support – Mawardy Nov 08 '20 at 20:04
44

See the discussion How to get GPS coordinates in the browser

There you have the link to example application Where Am I? that works on mobile devices. I've tested it on my Android and it started GPS module to get current coordinates. You can analyze the source on the living example.

I've made QR code for quick access from mobile:

Where Am I QR code

Danubian Sailor
  • 1
  • 38
  • 145
  • 223
21

To give a bit more specific answer. HTML5 allows you to get the geo coordinates, and it does a pretty decent job. Overall the browser support for geolocation is pretty good, all major browsers except ie7 and ie8 (and opera mini). IE9 does the job but is the worst performer. Checkout caniuse.com:

http://caniuse.com/#search=geol

Also you need the approval of your user to access their location, so make sure you check for this and give some decent instructions in case it's turned off. Especially for Iphone turning permissions on for Safari is a bit cumbersome.

Tosh
  • 1,789
  • 15
  • 20
  • http://www.dotnetcurry.com/aspnet-mvc/782/html5-geolocation-aspnet-mvc-part1 is a handy reference for implementation in asp.net mvc environment – Walter de Jong Mar 27 '17 at 00:25
15

Use this, and you will find all informations at http://www.w3schools.com/html/html5_geolocation.asp

<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>
Ema.H
  • 2,862
  • 3
  • 28
  • 41
  • 1
    Just a note: getCurrentPosition() and watchPosition() no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. – user2589273 Jan 01 '18 at 17:20
4

There is the GeoLocation API, but browser support is rather thin on the ground at present. Most sites that care about such things use a GeoIP database (with the usual provisos about the inaccuracy of such a system). You could also look at third party services requiring user cooperation such as FireEagle.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

Observable

/*
  function geo_success(position) {
    do_something(position.coords.latitude, position.coords.longitude);
  }

  function geo_error() {
    alert("Sorry, no position available.");
  }

  var geo_options = {
    enableHighAccuracy: true,
    maximumAge        : 30000,
    timeout           : 27000
  };

  var wpid = navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);
  */
  getLocation(): Observable<Position> {
    return Observable.create((observer) => {
      const watchID = navigator.geolocation.watchPosition((position: Position) => {
        observer.next(position);
      });
      return () => {
        navigator.geolocation.clearWatch(watchID);
      };
    });
  }

  ngOnDestroy() {
    this.sub.unsubscribe();
  }
Rusty Rob
  • 16,489
  • 8
  • 100
  • 116
1

Let's use the latest fat arrow functions:

navigator.geolocation.getCurrentPosition((loc) => {
  console.log('The location in lat lon format is: [', loc.coords.latitude, ',', loc.coords.longitude, ']');
})
Zameer Ansari
  • 28,977
  • 24
  • 140
  • 219