如何使用phonegap獲取位置資訊?

ADVERTISEMENT

如何使用phonegap獲取位置資訊?

例項如下:

<!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獲取位置資訊的實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援路飯。

ADVERTISEMENT