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?


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


  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) => {;
      return () => {

  ngOnDestroy() {

@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

var x = document.getElementById("demo");
function getLocation() {
    if (navigator.geolocation) {
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude; 

@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) {

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: - With this it is possible to get the client's current location:…

@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

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… is a handy reference for implementation in mvc environment

@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

35 Answered Questions

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

79 Answered Questions

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

  • 2011-04-23 22:17:18
  • Walker
  • 5763051 View
  • 7191 Score
  • 79 Answer
  • Tags:   javascript arrays

38 Answered Questions

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

20 Answered Questions

[SOLVED] Get the current URL with JavaScript?

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

38 Answered Questions

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

29 Answered Questions

[SOLVED] JavaScript post request like a form submit

16 Answered Questions

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

31 Answered Questions

[SOLVED] Detecting a mobile browser

17 Answered Questions

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

1 Answered Questions

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

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

Sponsored Content