0

Geolocation API に問題があります -.-'

シンプルな html5 アプリを作成するために、FirefoxOS Boilerplate アプリ ( https://github.com/robnyman/Firefox-OS-Boilerplate-App ) を使用しています。

問題は単純です。API から取得したデータ (緯度/経度) を関数によって配列として返したいのです。私が見つけたすべての例は、オンザフライでデータを使用してマップを表示したり、div に挿入したりします (このボイラープレートも同様です)。

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

これは、Geolocation のボイラープレートのコードです...

データを返す get_location のような関数が必要ですが、何日にもわたるテスト/Google 検索の後、私はあきらめました。

私が評価したオプションは、データを非表示のdivに保存するか、ローカルストレージ/クッキーで保存します。

助けてくれてありがとう!

編集 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());

コメントの後の新しいコード。このソリューションのパフォーマンスがわかりません... console.log をアラートに置き換えましたが、未定義になり、場合によっては非同期ではありません。

編集: 22/11: while を修正

4

1 に答える 1