Get latitude and longitude values from GeoLocation.observe

This is a difficulty I have with Javascript. I know this is a trivial and boring question but it’s too long that I’m trying without success.

How do I get the values of latitude and longitude from the following code (taken from the Geolocation docs)?

var continuousLocation = GeoLocation.observe("changed").map(JSON.stringify);

For example, for the immediateLocation I may do

var myLatitude    = GeoLocation.location.latitude;
var myLongitude   = GeoLocation.location.longitude;

But I don’t know how to do the same for continuousLocation.

It’s async.

If you need the values in JavaScript, you could do something like this:

var continuousLocation = GeoLocation.observe("changed").map(JSON.stringify);
continuousLocation.onValueChanged(module, function(x) {
    console.log("The value of continuousLocation is now: " + x);
});

Hope this helps!

Thank you for the prompt reply. Unfortunately the answer did not help. I would like to get the single values of latitude and longitude from the string continuousLocation so I can use them to set a Marker on a map:

<MapMarker Latitude="{latitude}" Longitude="{longitude}" />

I tried x.latitude or response = JSON.parse(x); to access the values as response.latitude but I’m missing something.

The GeoLocation docs have a fairly straightforward solution for you:

Quoting:

In the above example we’re using the EventEmitter observe method to create an Observable from the “changed” event. We can also listen to changes by using the on method, as follows:

GeoLocation.on("changed", function(location) { ... })

Locations returned by this module are JavaScript objects of the following form:

{
    latitude: a number measured in decimal degrees,
    longitude: a number measured in decimal degrees,
    accuracy: a number measured in meters
}

Applied to your code then:

var lat = Observable();
var lon = Observable();
GeoLocation.on("changed", function(location) {
    lat.value = location.latitude;
    lon.value = location.longitude;
});
module.exports = {
    latitude: lat,
    longitude: lon
};

Thank you Uldis! It works perfectly.