<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Compass Example</title>
<script type="text/javascript" charset="UTF-8" src="cordova.js"></script>
<script type="text/javascript" charset="UTF-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
getPosition();
}
//獲取當(dāng)前位置信息
function getPosition() {
navigator.geolocation.getCurrentPosition(onSuccess,onError);
}
//錯(cuò)誤的回調(diào)
function onError(error) {
alert('code:'+error.code+'\n'+'message:'+error.message+'\n');
}
//成功的回調(diào)
function onSuccess(position) {
alert('Latitude:'+position.coords.latitude+'\n'+
'Longitude:'+position.coords.longitude+'\n'+
'Altitude:'+position.coords.altitude+'\n'+
'Accuracy:'+position.coords.accuracy+'\n'+
'Altitude Accuracy:'+position.coords.altitudeAccuracy+'\n'+
'Heading:'+position.coords.heading+'\n'+
'Speed:'+position.coords.speed+'\n'+
'Timestamp:'+new Date(position.timeStamp)+'\n');
}
</script>
</head>
<body>
<button onclick="getPosition();">Start Watching</button>
</body>
</html>
以上這篇使用phonegap獲取位置信息的實(shí)現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。