0

i have a problem with the Geolocation API -.-'

I'm using the FirefoxOS Boilerplate App (https://github.com/robnyman/Firefox-OS-Boilerplate-App) for create a simple html5 app.

The problem is simple: I would like the data (lat/lon) obtained from the API are returned by a function as an array. All the examples that i have found uses the data on the fly for show the maps or insert in a div (as also this boilerplate).

navigator.geolocation.getCurrentPosition(function (position) {
            geolocationDisplay.innerHTML = "<strong>Latitude:</strong> " + position.coords.latitude + ", <strong>Longitude:</strong> " + position.coords.longitude;
            geolocationDisplay.style.display = "block";
        },
        function (position) {
            geolocationDisplay.innerHTML = "Failed to get your current location";
            geolocationDisplay.style.display = "block";
        });

This is the code of the boilerplate for the Geolocation...

I would want a function like get_location that return me the data, but after days of testing/google search I gave up and I ask you who are more experienced with callback/scope in Javascript of me.

The opntions that i have evaluated it's save the data in a hidden div or save with localstorage/cookies.

Thanks for the help!

EDIT 20/11:

    function load_location() {
        navigator.geolocation.getCurrentPosition(save_location, handleLocationError, {maximumAge: 0, timeout: 1000, enableHighAccuracy: true});
    }

    function handleLocationError(error) {
        alert(error.code + ' - ' + error.message);
    }

    function save_location(position) {
        localStorage.clear();
        ls_save('latitude',position.coords.latitude);
        ls_save('longitude',position.coords.longitude);
        ls_save('accuracy',position.coords.accuracy);
        ls_save('altitude',position.coords.altitude);
        ls_save('altitudeAccuracy',position.coords.altitudeAccuracy);
        ls_save('heading',position.coords.heading);
        ls_save('speed',position.coords.speed);

    }

    function ls_save(key,value) {
        localStorage.setItem(key, value); 
    }

    function get_location() {
    while(typeof localStorage['latitude'] === 'string') {
        return localStorage.getItem("latitude");
    }
}

    load_location();
//Code
    console.log(get_location());

The new code after the comments. I do not know how performance this solution... I have replaced console.log with alert and i get undefined then in some cases is not asynchronous.

Edit: 22/11: Fixed the while

Mte90
  • 115
  • 3
  • 13

1 Answers1

1

You can return the geolocation data as an array, by doing something like this:

function doSomethingWithGeo(geo) {
    console.log(geo);
}

function get_location() {
    navigator.geolocation.getCurrentPosition(function (position) {
        doSomethingWithGeo([[position.coords.latitude, position.coords.longitude]]);
    });
}

When you call get_location, it will retrieve the geolocation coordinates, and will call the function doSomethingWithGeo with the array you wanted. If you want to store your data, you can do it in the doSomethingWithGeo function.

Let me know if it's not what you were looking for.

fharper
  • 559
  • 2
  • 13
  • uhm like this: `function save_location(geo) { //save the data with cookie,local-storage, data tag ecc } function get_location() { //return the data} function load_location() { navigator.geolocation.getCurrentPosition(function (position) { save_location([[position.coords.latitude, position.coords.longitude]]); }); }` the only problem that is async, i have to think a solution for wait the response in get_location – Mte90 Nov 20 '13 at 13:15
  • You don't have too as in your code example, the anonymous function you pass to getCurrentPosition will be call only on success, so only when you'll be able to get the geolocation.In that case, save_location will be call only when you'll get the coordinates. Same for my doSomethingWithGeo function in my example. Try it, you'll see what happen. – fharper Nov 20 '13 at 14:50
  • As I said previously, you can't call get_location right after trying to load the location as getCurrentPosition is asynchronous, so it's why you need a callback function. You either call get_location from the callback function, or check if there is data saved before trying to retrieve it. – fharper Dec 05 '13 at 20:08