1

地理位置情報に応じて、いくつかの JavaScript コードがあります。

http://jsfiddle.net/8D6vz/

var myLat = 0.0;
var myLng = 0.0;

function setCurrentPosition(position) {
 myLat = position.coords.latitude;
 myLng = position.coords.longitude;
}

function errorOccured() {
 document.write("sorry, error occured :( ");
}

$(function() {
 navigator.geolocation.getCurrentPosition(setCurrentPosition, errorOccured);
 document.write(myLat + " " + myLng);
});

ただし、このコードは単に生成します

0 0

それ以外の

"client's latitude here" "client's longitude here"

なんで?

地理位置情報を確実にサポートする Google Chrome を使用しています。また、ブラウザが自分の位置を追跡できるようにしました。</p>

4

2 に答える 2

2

GetCurrentPositionは非同期関数であり、次のように使用できます。

function GetGeolocation() {

navigator.geolocation.getCurrentPosition(GetCoords, GetError);

}
function GetCoords(position){

  var lat = position.coords.latitude;

  var long = position.coords.longitude;

  var accuracy = position.coords.accuracy;

  alert('Latitude: ' + lat + ', Longitude: '+ long);

}
于 2012-03-20T07:25:11.550 に答える
0

ああ、私はそれを理解したと思います。

私は信じている

navigator.geolocation.getCurrentPosition()

非同期関数です。したがって、setCurrentPosition(position)緯度と経度の値が画面に出力された後に呼び出され、元の値が出力されます。

于 2012-03-20T05:11:09.160 に答える