By Ganesh


2010-04-05 07:13:14 8 Comments

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?

7 comments

@robert king 2018-04-18 07:21:16

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();
  }

@student 2018-04-16 10:42:28

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, ']');
})

@Ema.H 2016-11-25 09:49:55

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>

@Ciasto piekarz 2017-08-19 14:10:03

tested from iphone works very precise (y)

@user2589273 2018-01-01 17:20:41

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.

@dochoffiday 2010-04-05 20:43:13

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.

@Shane N 2012-02-24 02:04:56

Is the Google Client Location API mentioned still around? The link provided goes to Google Loader

@dochoffiday 2012-02-24 15:42:49

I don't see it on that page as one of the available APIs. - You may be able to use to Google Latitude API: code.google.com/apis/latitude - With this it is possible to get the client's current location: code.google.com/apis/latitude/v1/…

@surfealokesea 2013-07-05 08:48:33

@johnnyRose 2015-12-29 19:23:01

For future readers, the accuracy is measured in meters.

@Tarzan 2016-04-30 05:35:52

Hyy can we enable the sharing location autmatically via coding

@Rishul Matta 2016-06-07 15:05:12

this doesnt work on http websites isnt it?

@Onur Yıldırım 2016-07-01 23:54:42

@Jack 2017-09-17 23:51:33

Does this request the use of your location?

@Tosh 2012-08-23 21:58:54

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.

@Walter de Jong 2017-03-26 22:13:07

@Walter de Jong 2017-03-27 00:25:17

dotnetcurry.com/aspnet-mvc/782/… is a handy reference for implementation in asp.net mvc environment

@Danubian Sailor 2011-11-16 10:41:59

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

@Asken 2012-08-03 05:34:08

+1 for the QR code...

@captDaylight 2013-07-30 20:07:28

that QR saved me a whole bunch of effort

@Roni Tovi 2014-11-22 13:28:51

does not work on my Android phone even when GPS + WiFi is enabled and allowed.

@kiamlaluno 2016-05-21 12:42:37

The link Where Am I? returns a 404 error.

@Shittu Joseph Olugbenga 2018-04-21 06:57:15

QR code link broken.

@Sumurai8 2018-10-26 09:43:55

@danubian Please include relevant information from the first link in this post, and delete the rest of the text as the links are broken.

@Rajdeep Singh 2018-10-26 09:46:03

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review

@Quentin 2010-04-05 07:22:59

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.

@Ganesh 2010-04-05 07:33:34

thanks alot for your quick response. if possible, can you provide me some more tips...

Related Questions

Sponsored Content

37 Answered Questions

[SOLVED] How do I remove a property from a JavaScript object?

17 Answered Questions

[SOLVED] Get the size of the screen, current web page and browser window

31 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

17 Answered Questions

[SOLVED] How to decide when to use Node.js?

69 Answered Questions

[SOLVED] How do I remove a particular element from an array in JavaScript?

  • 2011-04-23 22:17:18
  • Walker
  • 5215250 View
  • 6520 Score
  • 69 Answer
  • Tags:   javascript arrays

20 Answered Questions

[SOLVED] Get the current URL with JavaScript?

  • 2009-06-23 19:26:45
  • dougoftheabaci
  • 2428416 View
  • 2597 Score
  • 20 Answer
  • Tags:   javascript url

28 Answered Questions

[SOLVED] JavaScript post request like a form submit

30 Answered Questions

[SOLVED] Detecting a mobile browser

36 Answered Questions

[SOLVED] How do you get a timestamp in JavaScript?

1 Answered Questions

[SOLVED] How to access GPS location from the web browser

  • 2015-03-12 05:26:58
  • Sanju B Myh
  • 625 View
  • -1 Score
  • 1 Answer
  • Tags:   ruby gps geolocation

Sponsored Content