edmckee / google-maps-utility-library-v3

Automatically exported from code.google.com/p/google-maps-utility-library-v3
Apache License 2.0
0 stars 0 forks source link

[Geolocation Marker] Get user coordinates? #275

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am using the Geolocation marker (excellent work btw!) and I would like to add 
an info window displaying the user's coordinates (as indicated by the marker on 
the map). How can I get the user's current coordinates and display them?

Original issue reported on code.google.com by i...@cityhunters.de on 31 Oct 2013 at 10:28

GoogleCodeExporter commented 9 years ago
Like so:

  var geomarker = new GeolocationMarker();
  ...
  var currentPosition = geomarker.getPosition();
  console.log(currentPosition.toUrlValue());

However keep in mind that getPosition() returns null when a position fix hasn't 
yet been obtained.

If you want to display live coordinates, you would bind to the MVC event.

  google.maps.event.addListener(geomarker, "position_changed", function() {
    //update user display
  });

The GeolocationMarker is an MVCObject and can be used as an anchor for the 
InfoWindow class. This way, the infowindow will move with the marker.

Original comment by chadkill...@missouristate.edu on 31 Oct 2013 at 1:22

GoogleCodeExporter commented 9 years ago
Thanks for the hint! Makes perfect sense.
Unfortunately I'm a bit of a newbie to Google API programming...
 So can you explain more in detail how I would need to change the code in order to display the current user coordinates in real-time? 
Also I may have misused the word "info window" in this context - I didn't 
actually refer to the fixed term InfoWindow in the context of API programming. 
The window could as well be a simple CSS container positioned somewhere outside 
the map.
What I actually want to achieve is getting the Lat/Long values extracted from 
the Geolocation script and have them displayed (using Javascript) anywhere on 
the screen.

The goal would be to get the user's coordinate and 1. have it displayed 
somewhere on the screen and 2. use it in connection with the following distance 
measuring script in order to display the distance to another (fixed) marker as 
well.

function distance(lat1,lon1,lat2,lon2) {
    var R = 6371; // km
    var dLat = (lat2-lat1) * Math.PI / 180;
    var dLon = (lon2-lon1) * Math.PI / 180; 
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1 * Math.PI / 180 ) * Math.cos(lat2 * Math.PI / 180 ) * 
        Math.sin(dLon/2) * Math.sin(dLon/2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var d = R * c;
    if (d>1) return Math.round(d)+"km";
    else if (d<=1) return Math.round(d*1000)+"m";
    return d;
}

Original comment by i...@cityhunters.de on 31 Oct 2013 at 1:50

GoogleCodeExporter commented 9 years ago
Since this is the issue tracker for the project (and this really isn't a bug or 
feature request), can you ask the question on StackOverflow?

I monitor the [google-maps-api-3] tag and will be happy to answer your question 
there where we can use code formatting and syntax highlighting.

Original comment by chadkill...@missouristate.edu on 31 Oct 2013 at 2:25

GoogleCodeExporter commented 9 years ago
I thought the same. Sorry for bothering you here. Posted the question on 
Stackoverflow: 
http://stackoverflow.com/questions/19709831/show-users-coordinates-in-geolocatio
n-api-and-trigger-event-in-a-certain-area 

Original comment by i...@cityhunters.de on 31 Oct 2013 at 2:57