緯度と経度で情報 (州、国、住所) を取得するには、Google (または同様の) ジオコード API を使用する必要があります。
私はたくさんのグーグル検索を行い、いくつかの有用なコードを見つけましたが、このコードは情報を警告ボックスに入れます.
コールバック関数に関するヒントを見つけました...
非同期タスクの外部で「状態」または「国」を使用する必要があります (たとえば、ボタンを押したときの HTML ページで)。
グローバル変数 ( actArea ) を作成しようとしましたが、 navigator.geolocation.getCurrentPosition呼び出しの後、その値は「EMPTY」です。Async Task は非同期なので、これは正しいと思いますが、Async Task の結果をグローバル変数などに保存するにはどうすればよいですか?
コールバックが機能しないのはなぜですか? 間違いはどこですか?
setTimeoutも使用してみましたが、結果は同じです。
ありがとう。
これは私のコードの一部です ( geodec.js ):
var x = document.getElementById("destination_div");
var actLatPos = 45.076947;
var actLonPos = 7.614174;
var actArea = 'EMPTY';
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition,showError);
//actArea returns "EMPTY"
alert(actArea);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function retrieveGPSdata(data)
{
actArea=data.results[0].address_components[5].long_name;
//actArea returns "Piemonte"
alert(actArea);
$('#destination_div').text( "Latitude: " + actLatPos + " Longitude: " + actLonPos + " Area: " + actArea);
}
function showPosition(position) {
var richiesta='http://maps.googleapis.com/maps/api/geocode/json?latlng=' + actLatPos + ',' + actLonPos + '&sensor=true';
//setTimeout(function()
//{
$.ajax({ url:richiesta, success:retrieveGPSdata});
//},8000);
}