geolocation对象提供了对设备GPS传感器的访问。
Geolocation提供设备的位置信息,例如经度和纬度。位置信息的常见来源包括全球定位系统(GPS),以及通过诸如IP地址、RFID、WiFi和蓝牙的MAC地址、和GSM/CDMA手机ID的网络信号所做的推断。不能保证该API返回的是设备的真实位置信息。
这个API是基于W3C Geo location API Specification实现的。有些设备已经提供了对该规范的实现,对于这些设备采用内置实现而非使用PhoneGap的实现。对于没有地理位置支持的设备,PhoneGap的实现应该是完全兼容W3C规范。

方法:
  • geolocation.getCurrentPosition
  • geolocation.watchPosition
  • geolocation.clearWatch

参数:
  • geolocationSuccess
  • geolocationError
  • geolocationOptions

对象(只读):
  • Position
  • PositionError
  • Coordinates

geolocation.getCurrentPosition

返回一个Position对象表示设备的当前位置。
  1. navigator.geolocation.getCurrentPosition(geolocationSuccess,
  2. [geolocationError],
  3. [geolocationOptions]);
复制代码
参数:
  • geolocationSuccess:获取位置信息成功时调用的回调函数,参数为当前的位置信息。
  • geolocationError:(可选项)获取位置信息出错时调用的回调函数。
  • geolocationOptions:(可选项)地理位置选项。

说明:
geolocation.getCurrentPositon是一个异步函数。它回传一个包含设备当前位置信息的Position对象给geolocationSuccess回调函数。如果发生错误,触发geolocationError回调函数并传递一个PositionError对象。

支持的平台:
  • Android
  • BlackBerry (OS 4.6)
  • BlackBerry WebWorks (OS 5.0或更高版本)
  • iPhone

简单的范例:
  1. //获取位置信息成功时调用的回调函数
  2. //该方法接受一个“Position”对象,包含当前GPS坐标信息
  3. var onSuccess = function(position) {
  4. alert('Latitude: ' + position.coords.latitude + '\n' +
  5. 'Longitude: ' + position.coords.longitude + '\n' +
  6. 'Altitude: ' + position.coords.altitude + '\n' +
  7. 'Accuracy: ' + position.coords.accuracy + '\n' +
  8. 'Altitude Accuracy: ' + position.coords.altitudeAccuracy+ '\n' +
  9. 'Heading: ' + position.coords.heading + '\n' +
  10. 'Speed: ' + position.coords.speed + '\n' +
  11. 'Timestamp: ' + new Date(position.timestamp) + '\n');
  12. };

  13. // onError回调函数接收一个PositionError对象
  14. function onError(error) {
  15. alert('code: ' + error.code + '\n' +
  16. 'message: ' + error.message + '\n');
  17. }

  18. navigator.geolocation.getCurrentPosition(onSuccess, onError);
复制代码 完整的范例:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Device Properties Example</title>

  5. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
  6. <script type="text/javascript" charset="utf-8">

  7. // 等待加载PhoneGap
  8. document.addEventListener("deviceready", onDeviceReady, false);

  9. // PhoneGap加载完毕
  10. function onDeviceReady() {
  11. navigator.geolocation.getCurrentPosition(onSuccess, onError);
  12. }

  13. // 获取位置信息成功时调用的回调函数
  14. function onSuccess(position) {
  15. var element = document.getElementById('geolocation');
  16. element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
  17. 'Longitude: ' + position.coords.longitude + '<br />' +
  18. 'Altitude: ' + position.coords.altitude + '<br />' +
  19. 'Accuracy: ' + position.coords.accuracy + '<br />' +
  20. 'Altitude Accuracy: '+ position.coords.altitudeAccuracy + '<br />' +
  21. 'Heading: ' + position.coords.heading + '<br />' +
  22. 'Speed: ' + position.coords.speed + '<br />' +
  23. 'Timestamp: ' + new Date(position.timestamp) + '<br />';
  24. }

  25. // onError回调函数接收一个PositionError对象
  26. function onError(error) {
  27. alert('code: ' + error.code + '\n' +
  28. 'message: ' + error.message + '\n');
  29. }

  30. </script>
  31. </head>
  32. <body>
  33. <p id="geolocation">Finding geolocation...</p>
  34. </body>
  35. </html>
复制代码
geolocation.watchPosition
监视设备的当前位置的变化。


  1. var watchId = navigator.geolocation.watchPosition(geolocationSuccess,
  2. [geolocationError],
  3. [geolocationOptions]);
复制代码
参数:
  • geolocationSuccess:获取位置信息成功时调用的回调函数,参数为当前位置信息。
  • geolocationError:(可选项)获取位置信息出错时调用的回调函数。
  • geolocationOptions:(可选项)地理位置选项。

返回值
  • String:返回的watch id是位置监视String:返回的watch id是位置监视周期的引用。可以通过geolocation.clearWatch调用该watch ID以停止对位置变化的监视。

说明:
geolocation.watchPosition是一个异步函数。当检测到设备的位置发生改变时,它返回设备的当前位置。当设备检索到一个新的位置,会触发geolocationSuccess回调函数并传递一个Position对象作为参数。如果发生错误,会触发geolocationError回调函数并传递一个PositionError对象。

支持的平台:
  • Android
  • BlackBerry (OS 4.6)
  • BlackBerry WebWorks (OS 5.0或更高版本)
  • iPhone

简单的范例:
  1. // 获取位置信息成功时调用的回调函数
  2. // 该方法接受一个“Position”对象,包含当前GPS坐标信息
  3. function onSuccess(position) {
  4. var element = document.getElementById('geolocation');
  5. element.innerHTML = 'Latitude: '+ position.coords.latitude + '<br>' +
  6. 'Longitude: ' + position.coords.longitude + '<br>' +
  7. '<hr>' + element.innerHTML;
  8. }

  9. // onError回调函数接收一个PositionError对象
  10. function onError(error) {
  11. alert('code: ' + error.code + '\n' +
  12. 'message: ' + error.message + '\n');
  13. }

  14. // Options: 每隔3秒钟检索一次位置信息
  15. var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { frequency: 3000 });
复制代码 完整的范例:





  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Device Properties Example</title>

  5. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
  6. <script type="text/javascript" charset="utf-8">

  7. // 等待加载PhoneGap
  8. document.addEventListener("deviceready", onDeviceReady, false);

  9. var watchID = null;

  10. // PhoneGap加载完毕
  11. function onDeviceReady() {
  12. // 每隔3秒钟更新一次
  13. var options = { frequency: 3000 };
  14. watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
  15. }

  16. // 获取位置信息成功时调用的回调函数
  17. function onSuccess(position) {
  18. var element = document.getElementById('geolocation');
  19. element.innerHTML = 'Latitude: '+ position.coords.latitude + '<br />' +
  20. 'Longitude: ' + position.coords.longitude + '<br />' +
  21. <hr />'' + element.innerHTML;
  22. }

  23. // onError回调函数接收一个PositionError对象
  24. function onError(error) {
  25. alert('code: ' + error.code + '\n' +
  26. 'message: ' + error.message + '\n');
  27. }

  28. </script>
  29. </head>
  30. <body>
  31. <p id="geolocation">Finding geolocation...</p>
  32. </body>
  33. </html>
复制代码 geolocation.clearWatch
停止watchID参数指向的设备位置变化监
  1. navigator.geolocation.clearWatch(watchID);
复制代码
参数:
  • watchID:要清除的watchPosition周期的id。(字符串类型)

说明:
geolocation.clearWatch函数通过清除watchID指向的geolocation.watchPosition来停止对设备位置变化的监视。

支持的平台:
  • Android
  • BlackBerry (OS 4.6)
  • BlackBerry WebWorks (OS 5.0或更高版本)
  • iPhone

简单的范例:
  1. // 选项: 每隔3秒钟检索一次位置信息
  2. var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { frequency: 3000 });

  3. // ...后继处理...

  4. navigator.geolocation.clearWatch(watchID);
复制代码
完整的范例:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Device Properties Example</title>

  5. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
  6. <script type="text/javascript" charset="utf-8">

  7. // 等待加载PhoneGap
  8. document.addEventListener("deviceready", onDeviceReady, false);

  9. var watchID = null;

  10. // PhoneGap加载完毕
  11. function onDeviceReady() {
  12. // 每隔3秒钟更新一次
  13. var options = { frequency: 3000 };
  14. watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
  15. }

  16. // 获取位置信息成功时调用的回调函数
  17. function onSuccess(position) {
  18. var element = document.getElementById('geolocation');
  19. element.innerHTML = 'Latitude: '+ position.coords.latitude + '<br />' +
  20. 'Longitude: ' + position.coords.longitude + '<br />' +
  21. '<hr />' + element.innerHTML;
  22. }

  23. // 清除前述已经开始的监视
  24. function clearWatch() {
  25. if (watchID != null) {
  26. navigator.geolocation.clearWatch(watchID);
  27. watchID = null;
  28. }
  29. }

  30. // onError回调函数接收一个PositionError对象
  31. function onError(error) {
  32. alert('code: ' + error.code + '\n' +
  33. 'message: ' + error.message + '\n');
  34. }

  35. </script>
  36. </head>
  37. <body>
  38. <p id="geolocation">Finding geolocation...</p>
  39. <button onclick="clearWatch();">Clear Watch</button>
  40. </body>
  41. </html>
复制代码
geolocationSuccess
当得到一个有效地理位置信息时,此用户回调函数被调当获得一个地理位置信息时,此用户回调函数被调用。


  1. function(position) {
  2. // 进行处理
  3. }
复制代码
参数:
  • position:设备返回的地理位置信息。(Position类型)

范例:
  1. function geolocationSuccess(position) {
  2. alert('Latitude: ' + position.coords.latitude + '\n' +
  3. 'Longitude: ' + position.coords.longitude + '\n' +
  4. 'Altitude: ' + position.coords.altitude + '\n' +
  5. 'Accuracy: ' + position.coords.accuracy + '\n' +
  6. 'Altitude Accuracy: ' + position.coords.altitudeAccuracy+ '\n' +
  7. 'Heading: ' + position.coords.heading + '\n' +
  8. 'Speed: ' + position.coords.speed + '\n' +
  9. 'Timestamp: ' + new Date(position.timestamp) + '\n');
  10. }
复制代码
geolocationError
当geolocation函数发生错误时,此用户回调函数被调用。


  1. function(error) {
  2. // 处理错误
  3. }
复制代码
参数:
  • error:设备返回的错误信息。(PositionError类型)

geolocationOptions

用户定制地理位置检索的可选参数。
  1. { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true };
复制代码
选项:
  • frequency:以毫秒为单位的检索位置周期。这个选项并非W3C规范的一部分,未来会被删除并用maximumAge来替代该选项。(数字类型)(默认值:10000)
  • enableHighAccuracy:提供一个表明应用程序希望获得最佳可能结果的提示。(布尔类型)
  • timeout:允许的以毫秒为单位的最大时间间隔,该时间间隔是从geolocation.getCurrentPosition或geolocation.watchPosition的调用到相应的geolocationSuccess回调函数被调用。(数字类型)
  • maximumAge:应用程序将接受一个缓存的位置信息,当该缓存的位置信息的年龄不大于此参数设定值,单位是毫秒。(数字类型)

Android的特异情况:
除非enableHighAccuracy选项被设定为true,否则Android 2.X模拟器不会返回一个地理位置结果。
  1. { enableHighAccuracy: true }
复制代码
Position
包含由geolocation API创建的Position坐标信息。

属性:
  • coords:一系列地理坐标。(Coordinates类型)
  • timestamp:以毫秒为单位的coords的创建时间戳。(DOMTimeStamp类型)

说明:
Position对象是由PhoneGap创建和填充的,并通过一个回调函数返回用户。

支持的平台:
  • Android
  • BlackBerry (OS 4.6)
  • BlackBerry WebWorks (OS 5.0或更高版本)
  • iPhone

简单的范例:
  1. // 获取位置信息成功后调用的回调函数
  2. var onSuccess = function(position) {
  3. alert('Latitude: ' + position.coords.latitude + '\n' +
  4. 'Longitude: ' + position.coords.longitude + '\n' +
  5. 'Altitude: ' + position.coords.altitude + '\n' +
  6. 'Accuracy: ' + position.coords.accuracy + '\n' +
  7. 'Altitude Accuracy: ' + position.coords.altitudeAccuracy+ '\n' +
  8. 'Heading: ' + position.coords.heading + '\n' +
  9. 'Speed: ' + position.coords.speed + '\n' +
  10. 'Timestamp: ' + new Date(position.timestamp) + '\n');
  11. };

  12. // onError回调函数接收一个PositionError对象
  13. function onError(error) {
  14. alert('code: ' + error.code + '\n' +
  15. 'message: ' + error.message + '\n');
  16. }

  17. navigator.geolocation.getCurrentPosition(onSuccess, onError);
复制代码
完整的范例:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Device Properties Example</title>

  5. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
  6. <script type="text/javascript" charset="utf-8">

  7. // 等待加载PHoneGap
  8. document.addEventListener("deviceready", onDeviceReady, false);

  9. // PhoneGap加载完毕
  10. function onDeviceReady() {
  11. navigator.geolocation.getCurrentPosition(onSuccess, onError);
  12. }

  13. // 获取位置信息成功后调用的回调函数
  14. function onSuccess(position) {
  15. var element = document.getElementById('geolocation');
  16. element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
  17. 'Longitude: ' + position.coords.longitude + '<br />' +
  18. 'Altitude: ' + position.coords.altitude + '<br />' +
  19. 'Accuracy: ' + position.coords.accuracy + '<br />' +
  20. 'Altitude Accuracy: '+ position.coords.altitudeAccuracy + '<br />' +
  21. 'Heading: ' + position.coords.heading + '<br />' +
  22. 'Speed: ' + position.coords.speed + '<br />' +
  23. 'Timestamp: ' + new Date(position.timestamp) + '<br />';
  24. }

  25. // onError回调函数接收一个PositionError对象
  26. function onError(error) {
  27. alert('code: ' + error.code + '\n' +
  28. 'message: ' + error.message + '\n');
  29. }

  30. </script>
  31. </head>
  32. <body>
  33. <p id="geolocation">Finding geolocation...</p>
  34. </body>
  35. </html>
复制代码
iPhone的特异情况:
  • timestamp:单位为秒而非毫秒。


一种变通方法是手动将时间戳转换为毫秒(*1000):
  1. var onSuccess = function(position) {
  2. alert('Latitude: '+ position.coords.latitude + '\n' +
  3. 'Longitude: ' + position.coords.longitude + '\n' +
  4. 'Timestamp: ' + new Date(position.timestamp * 1000)+ '\n');
  5. };
复制代码
PositionError
当发生错误时,一个PositionError对象会传递给geolocationError回调函数。

属性:
  • code:一个在下面常量列表中定义的错误代码。
  • message:说明错误细节的错误信息。

常量:
  • PositionError.PERMISSIONPositionError.PERMISSION_DENIED:权限被拒绝
  • PositionError.POSITION_UNAVAILABLE:位置不可用
  • PositionError.TIMEOUT:超时

说明:
当使用Geolocation发生错误时,一个PositionError对象会作为geolocationError回调函数的参数传递给用户。

Coordinates


一系列用来描述位置的地理坐标信息的属性。

属性:
  • latitude:以十进制表示的纬度。(数字类型)
  • longitude:以十进制表示的经度。(数字类型)
  • altitude:位置相对于椭圆球面的高度,单位为米。(数字类型)
  • accuracy:以米为单位的纬度和经度坐标的精度水平。(数字类型)
  • altitudeAccuracy:以米为单位的高度坐标的精度水平。(数字类型)
  • heading:运动的方向,通过相对正北做顺时针旋转的角度指定。(数字类型)
  • speed:以米/秒为单位的设备当前地面速度。(数字类型)

说明:
作为Position对象的一部分,Coordinates对象是由PhoneGap创建和填充的。该Position对象会作为一个回调函数的参数返回用户。

支持的平台:
  • Android
  • BlackBerry (OS 4.6)
  • BlackBerry WebWorks (OS 5.0或更高版本)
  • iPhone

简单的范例:
  1. // 获取位置信息成功后调用的回调函数
  2. var onSuccess = function(position) {
  3. alert('Latitude: ' + position.coords.latitude + '\n' +
  4. 'Longitude: ' + position.coords.longitude + '\n' +
  5. 'Altitude: ' + position.coords.altitude + '\n' +
  6. 'Accuracy: ' + position.coords.accuracy + '\n' +
  7. 'Altitude Accuracy: ' + position.coords.altitudeAccuracy+ '\n' +
  8. 'Heading: ' + position.coords.heading + '\n' +
  9. 'Speed: ' + position.coords.speed + '\n' +
  10. 'Timestamp: ' + new Date(position.timestamp) + '\n');
  11. };

  12. // 获取位置信息出错后调用的回调函数
  13. var onError = function() {
  14. alert('onError!');
  15. };

  16. navigator.geolocation.getCurrentPosition(onSuccess, onError);
复制代码
完整的范例:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Geolocation Position Example</title>

  5. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
  6. <script type="text/javascript" charset="utf-8">

  7. // 设置一个当PhoneGap加载完毕后触发的事件
  8. document.addEventListener("deviceready", onDeviceReady, false);

  9. // PhoneGap加载完毕并就绪
  10. function onDeviceReady() {
  11. navigator.geolocation.getCurrentPosition(onSuccess, onError);
  12. }

  13. // 显示位置信息中的“Position”属性
  14. function onSuccess(position) {
  15. var div = document.getElementById('myDiv');

  16. div.innerHTML = 'Latitude: ' + position.coords.latitude+ '<br/>' +
  17. 'Longitude: ' + position.coords.longitude + '<br/>' +
  18. 'Altitude: ' + position.coords.altitude+ '<br/>' +
  19. 'Accuracy: ' + position.coords.accuracy+ '<br/>' +
  20. 'Altitude Accuracy: ' + position.coords.altitudeAccuracy+ '<br/>' +
  21. 'Heading: ' + position.coords.heading + '<br/>' +
  22. 'Speed: ' + position.coords.speed + '<br/>';
  23. }

  24. // 如果获取位置信息出现问题,则显示一个警告
  25. function onError() {
  26. alert('onError!');
  27. }

  28. </script>
  29. </head>
  30. <body>
  31. <div id="myDiv"></div></body>
  32. </html>
复制代码

更多相关文章

  1. [置顶] Android艺术开发探索学习 之 测量view的宽高 以及 动态设
  2. Android(安卓)Studio导入jar后无法识别、但项目能正常运行的问题
  3. Android实现信息安全中维吉尼亚密码技术
  4. Android编译系统环境初始化过程分析
  5. Android的Framework分析---4硬件抽象HAL
  6. 基于FFmpeg和SurfaceView实现Android原生窗口(ANativeWindow)的视
  7. Android(安卓)Dialog使用
  8. Android(安卓)动画框架详解,第 1 部分
  9. android分层学习笔记(二)

随机推荐

  1. Android(安卓)通知栏Notification总结一:
  2. Recovery模式的命令行参数
  3. android 调用系统命令实现关机
  4. LeakCanary使用详解
  5. Android调用第三方百度APP进行导航
  6. Android-EditText属性大全
  7. Android使用Recycler View实现瀑布流效果
  8. Android中测量文字的宽度和高度
  9. Android(安卓)Studio 常用控件和常用布局
  10. Android框架排行榜,上百项资源汇总不容错