實例如下:
<!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(); } //獲取當前位置信息 function getPosition() { navigator.geolocation.getCurrentPosition(onSuccess,onError); } //錯誤的回調 function onError(error) { alert('code:'+error.code+'\n'+'message:'+error.message+'\n'); } //成功的回調 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獲取位置信息的實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。