ジオロケーションを使用して、現在地の緯度と経度を取得しようとしています。最近まで非常によく似たコードが機能していたのですが、なぜ機能しなくなったのかわかりません。変数の緯度または経度は設定されなくなりました。jsコードをウォークスルーすると、getCurrentPosition()メソッドがスキップされ、理由がわかりません。奇妙なことに、getCurrentPosition()メソッドにアラートボックスを配置すると、緯度と経度が正しく取得されて表示されます...なぜそうなるのかわかりません。
var gl;
try {
if (typeof navigator.geolocation === 'undefined'){
gl = google.gears.factory.create('beta.geolocation');
} else {
gl = navigator.geolocation;
}
} catch(e) {}
var latitude;
var longitude;
if (gl) {
gl.getCurrentPosition(
function (position) {
latitude = position.coords.latitude;
longitude = position.coords.longitude;
//alert("got the lat & long - lat=" + latitude + ", lng=" + longitude);
},
function (error) {
alert("Error getting geolocation:" + error);
}
);
} else {
alert("Geolocation services are not supported by your web browser.");
}
次に、GoogleMapsAPIを使用して地図上にいくつかのマーカーを設定します。
どうもありがとうございます、
ピーター
編集
奇妙な振る舞いを示すJSFiddleのコードは次のとおりです。