这是我的:
var position;
navigator.geolocation.getCurrentPosition(onSuccess, onError);
function onSuccess(pos) {
position = { latitude: pos.coords.latitude, longitude: pos.coords.longitude} ;
//position = { latitude:43.465099,longitude:-80.520344};
}
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
var myOptions = {
center: new google.maps.LatLng(
position.latitude,position.longitude
),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true
};
当我使用注释掉的行时,程序运行良好并且缩放到硬编码位置。当我使用 pos.coords 行时,没有任何反应。我叫错了吗?我不能像这样把它放在我的变量中还是它会返回其他东西?
最佳答案
您似乎正确地使用了结果,但我会对 JSON 进行一些不同的编码:
position = { "latitude": pos.coords.latitude,
"longitude": pos.coords.longitude};
关于javascript - getCurrentPosition 返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9519080/