PhoneGap API帮助文档翻译Geolocation地理位置

移动开发
PhoneGap API帮助文档翻译Geolocation地理位置是本文要介绍的内容,主要是来了解并学习PhoneGap API文档的防疫,来看本文详解。

PhoneGap API帮助文档翻译Geolocation地理位置是本文要介绍的内容,主要是来了解并学习PhoneGap API文档的防疫,具体关于PhoneGap API文档的内容的学习来看本文详解,geolocation对象提供了对设备GPS传感器的访问。

Geolocation提供设备的位置信息,例如经度和纬度。位置信息的常见来源包括全球定位系统(GPS),以及通过诸如IP地址、RFID、WiFi和蓝牙的MAC地址、和GSM/CDMA手机ID的网络信号所做的推断。不能保证该API返回的是设备的真实位置信息。

这个API是基于W3C Geo location API Specification实现的。有些设备已经提供了对该规范的实现,对于这些设备采用内置实现而非使用PhoneGap的实现。对于没有地理位置支持的设备,PhoneGap的实现应该是完全兼容W3C规范。

方法:

  1. geolocation.getCurrentPosition  
  2. geolocation.watchPosition  
  3. geolocation.clearWatch 

参数:

  1. geolocationSuccess  
  2. geolocationError  
  3. geolocationOptions 

对象(只读):

  1. Position  
  2. PositionError  
  3. Coordinates  
  4. geolocation.getCurrentPosition   

返回一个Position对象表示设备的当前位置。

  1. navigator.geolocation.getCurrentPosition(geolocationSuccess,     
  2.                                 [geolocationError],     
  3.                                 [geolocationOptions]);    
  4. navigator.geolocation.getCurrentPosition(geolocationSuccess,   
  5.    [geolocationError],   
  6.    [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.     
  14. // onError回调函数接收一个PositionError对象    
  15. function onError(error) {    
  16.     alert('code: '    + error.code    + '\n' +    
  17.         'message: ' + error.message + '\n');    
  18. }    
  19.     
  20. navigator.geolocation.getCurrentPosition(onSuccess, onError);    
  21. // 获取位置信息成功时调用的回调函数  
  22. // 该方法接受一个“Position”对象,包含当前GPS坐标信息  
  23. var onSuccess = function(position) {  
  24.  alert('Latitude: '          + position.coords.latitude          + '\n' +  
  25.   'Longitude: '         + position.coords.longitude         + '\n' +  
  26.   'Altitude: '          + position.coords.altitude          + '\n' +  
  27.   'Accuracy: '          + position.coords.accuracy          + '\n' +  
  28.   'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +  
  29.   'Heading: '           + position.coords.heading           + '\n' +  
  30.   'Speed: '             + position.coords.speed             + '\n' +  
  31.   'Timestamp: '         + new Date(position.timestamp)      + '\n');  
  32. };  
  33.  
  34. // onError回调函数接收一个PositionError对象  
  35. function onError(error) {  
  36.  alert('code: '    + error.code    + '\n' +  
  37.   'message: ' + error.message + '\n');  
  38. }  
  39.  
  40. navigator.geolocation.getCurrentPosition(onSuccess, onError); 

完整的范例:

  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>        
  4. <title>Device Properties Example</title>    
  5.     
  6. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>    
  7. <script type="text/javascript" charset="utf-8">    
  8.     
  9.     // 等待加载PhoneGap    
  10.     document.addEventListener("deviceready", onDeviceReady, false);    
  11.         
  12.     // PhoneGap加载完毕    
  13.     function onDeviceReady() {    
  14.         navigator.geolocation.getCurrentPosition(onSuccess, onError);    
  15.     }    
  16.         
  17.     // 获取位置信息成功时调用的回调函数    
  18.     function onSuccess(position) {    
  19.         var element = document.getElementById('geolocation');    
  20.         element.innerHTML = 'Latitude: '           + position.coords.latitude              + '<br />' +    
  21.                             'Longitude: '          + position.coords.longitude             + '<br />' +    
  22.                             'Altitude: '           + position.coords.altitude              + '<br />' +    
  23.                             'Accuracy: '           + position.coords.accuracy              + '<br />' +    
  24.                             'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +    
  25.                             'Heading: '            + position.coords.heading               + '<br />' +    
  26.                             'Speed: '              + position.coords.speed                 + '<br />' +    
  27.                             'Timestamp: '          + new Date(position.timestamp)          + '<br />';    
  28.     }    
  29.         
  30.     // onError回调函数接收一个PositionError对象    
  31.     function onError(error) {    
  32.         alert('code: '    + error.code    + '\n' +    
  33.             'message: ' + error.message + '\n');    
  34.     }    
  35.     
  36. </script>    
  37. </head>    
  38. <body>    
  39.     <p id="geolocation">Finding geolocation...</p>    
  40. </body>    
  41. </html>    
  42. <!DOCTYPE html> 
  43. <html> 
  44. <head>   
  45. <title>Device Properties Example</title> 
  46.  
  47. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
  48. <script type="text/javascript" charset="utf-8"> 
  49.  
  50.  // 等待加载PhoneGap  
  51.  document.addEventListener("deviceready", onDeviceReady, false);  
  52.    
  53.  // PhoneGap加载完毕  
  54.  function onDeviceReady() {  
  55.   navigator.geolocation.getCurrentPosition(onSuccess, onError);  
  56.  }  
  57.    
  58.  // 获取位置信息成功时调用的回调函数  
  59.  function onSuccess(position) {  
  60.   var element = document.getElementById('geolocation');  
  61.   element.innerHTML = 'Latitude: '           + position.coords.latitude              + '<br />' +  
  62.        'Longitude: '          + position.coords.longitude             + '<br />' +  
  63.        'Altitude: '           + position.coords.altitude              + '<br />' +  
  64.        'Accuracy: '           + position.coords.accuracy              + '<br />' +  
  65.        'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +  
  66.        'Heading: '            + position.coords.heading               + '<br />' +  
  67.        'Speed: '              + position.coords.speed                 + '<br />' +  
  68.        'Timestamp: '          + new Date(position.timestamp)          + '<br />';  
  69.  }  
  70.    
  71.  // onError回调函数接收一个PositionError对象  
  72.  function onError(error) {  
  73.   alert('code: '    + error.code    + '\n' +  
  74.    'message: ' + error.message + '\n');  
  75.  }  
  76.  
  77. </script> 
  78. </head> 
  79. <body> 
  80.  <p id="geolocation">Finding geolocation...</p> 
  81. </body> 
  82. </html> 

geolocation.watchPosition

监视设备的当前位置的变化。

  1. var watchId = navigator.geolocation.watchPosition(geolocationSuccess,    
  2.                                          [geolocationError],    
  3.                                          [geolocationOptions]);    
  4. var watchId = navigator.geolocation.watchPosition(geolocationSuccess,  
  5. [geolocationError],  
  6. [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.     
  10. // onError回调函数接收一个PositionError对象    
  11. function onError(error) {    
  12.     alert('code: '    + error.code    + '\n' +    
  13.         'message: ' + error.message + '\n');    
  14. }    
  15.     
  16. // Options: 每隔3秒钟检索一次位置信息    
  17. var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { frequency: 3000 });    
  18. // 获取位置信息成功时调用的回调函数  
  19. // 该方法接受一个“Position”对象,包含当前GPS坐标信息  
  20. function onSuccess(position) {  
  21.  var element = document.getElementById('geolocation');  
  22.  element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br>' +  
  23.       'Longitude: ' + position.coords.longitude     + '<br>' +  
  24.       '<hr>' + element.innerHTML;   
  25. }  
  26.  
  27. // onError回调函数接收一个PositionError对象  
  28. function onError(error) {  
  29.  alert('code: '    + error.code    + '\n' +  
  30.   'message: ' + error.message + '\n');  
  31. }  
  32.  
  33. // Options: 每隔3秒钟检索一次位置信息  
  34. var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { frequency: 3000 });  
  35. 完整的范例:  
  36. view plaincopy to clipboardprint?<!DOCTYPE html>    
  37. <html>    
  38. <head>        
  39. <title>Device Properties Example</title>    
  40.     
  41. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>    
  42. <script type="text/javascript" charset="utf-8">    
  43.     
  44.     // 等待加载PhoneGap    
  45.     document.addEventListener("deviceready", onDeviceReady, false);     
  46.         
  47.     var watchID = null;    
  48.         
  49.     // PhoneGap加载完毕    
  50.     function onDeviceReady() {    
  51.         // 每隔3秒钟更新一次    
  52.         var options = { frequency: 3000 };    
  53.         watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);    
  54.     }    
  55.         
  56.     // 获取位置信息成功时调用的回调函数    
  57.     function onSuccess(position) {    
  58.         var element = document.getElementById('geolocation');    
  59.         element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +    
  60.                             'Longitude: ' + position.coords.longitude     + '<br />' +    
  61.                             <hr />''      + element.innerHTML;    
  62.     }    
  63.         
  64.     // onError回调函数接收一个PositionError对象    
  65.     function onError(error) {    
  66.         alert('code: '    + error.code    + '\n' +    
  67.             'message: ' + error.message + '\n');    
  68.     }    
  69.     
  70. </script>    
  71. </head>    
  72. <body>    
  73.     <p id="geolocation">Finding geolocation...</p>    
  74. </body>    
  75. </html>    
  76. <!DOCTYPE html> 
  77. <html> 
  78. <head>   
  79. <title>Device Properties Example</title> 
  80.  
  81. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
  82. <script type="text/javascript" charset="utf-8"> 
  83.  
  84.  // 等待加载PhoneGap  
  85.  document.addEventListener("deviceready", onDeviceReady, false);   
  86.    
  87.  var watchID = null;  
  88.    
  89.  // PhoneGap加载完毕  
  90.  function onDeviceReady() {  
  91.   // 每隔3秒钟更新一次  
  92.   var options = { frequency: 3000 };  
  93.   watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);  
  94.  }  
  95.    
  96.  // 获取位置信息成功时调用的回调函数  
  97.  function onSuccess(position) {  
  98.   var element = document.getElementById('geolocation');  
  99.   element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +  
  100.        'Longitude: ' + position.coords.longitude     + '<br />' +  
  101.        <hr />''      + element.innerHTML;  
  102.  }  
  103.    
  104.  // onError回调函数接收一个PositionError对象  
  105.  function onError(error) {  
  106.   alert('code: '    + error.code    + '\n' +  
  107.    'message: ' + error.message + '\n');  
  108.  }  
  109.  
  110. </script> 
  111. </head> 
  112. <body> 
  113.  <p id="geolocation">Finding geolocation...</p> 
  114. </body> 
  115. </html> 

geolocation.clearWatch 

停止watchID参数指向的设备位置变化监视。

  1. navigator.geolocation.clearWatch(watchID);    
  2. 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);    
  5. // 选项: 每隔3秒钟检索一次位置信息  
  6. var watchID = navigator.geolocation.watchPosition(onSuccess, onError, { frequency: 3000 });  
  7.  
  8. // ...后继处理...  
  9. navigator.geolocation.clearWatch(watchID); 

完整的范例:

  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>        
  4. <title>Device Properties Example</title>    
  5.     
  6. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>    
  7. <script type="text/javascript" charset="utf-8">    
  8.     
  9.     // 等待加载PhoneGap    
  10.     document.addEventListener("deviceready", onDeviceReady, false);     
  11.         
  12.     var watchID = null;    
  13.         
  14.     // PhoneGap加载完毕    
  15.     function onDeviceReady() {    
  16.         // 每隔3秒钟更新一次    
  17.         var options = { frequency: 3000 };    
  18.         watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);    
  19.     }    
  20.         
  21.     // 获取位置信息成功时调用的回调函数    
  22.     function onSuccess(position) {    
  23.         var element = document.getElementById('geolocation');    
  24.         element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +    
  25.                            'Longitude: ' + position.coords.longitude     + '<br />' +    
  26.                            '<hr />'      + element.innerHTML;    
  27.     }    
  28.         
  29.     // 清除前述已经开始的监视    
  30.     function clearWatch() {    
  31.         if (watchID != null) {    
  32.             navigator.geolocation.clearWatch(watchID);    
  33.             watchID = null;    
  34.         }    
  35.     }    
  36.         
  37.     // onError回调函数接收一个PositionError对象    
  38.     function onError(error) {    
  39.         alert('code: '    + error.code    + '\n' +    
  40.             'message: ' + error.message + '\n');    
  41.     }    
  42.     
  43. </script>    
  44. </head>    
  45. <body>    
  46.     <p id="geolocation">Finding geolocation...</p>    
  47.     <button onclick="clearWatch();">Clear Watch</button>    
  48. </body>    
  49. </html>    
  50. <!DOCTYPE html> 
  51. <html> 
  52. <head>   
  53. <title>Device Properties Example</title> 
  54.  
  55. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
  56. <script type="text/javascript" charset="utf-8"> 
  57.  
  58.  // 等待加载PhoneGap  
  59.  document.addEventListener("deviceready", onDeviceReady, false);   
  60.    
  61.  var watchID = null;  
  62.    
  63.  // PhoneGap加载完毕  
  64.  function onDeviceReady() {  
  65.   // 每隔3秒钟更新一次  
  66.   var options = { frequency: 3000 };  
  67.   watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);  
  68.  }  
  69.    
  70.  // 获取位置信息成功时调用的回调函数  
  71.  function onSuccess(position) {  
  72.   var element = document.getElementById('geolocation');  
  73.   element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +  
  74.          'Longitude: ' + position.coords.longitude     + '<br />' +  
  75.          '<hr />'      + element.innerHTML;  
  76.  }  
  77.    
  78.  // 清除前述已经开始的监视  
  79.  function clearWatch() {  
  80.   if (watchID != null) {  
  81.    navigator.geolocation.clearWatch(watchID);  
  82.    watchID = null;  
  83.   }  
  84.  }  
  85.    
  86.  // onError回调函数接收一个PositionError对象  
  87.  function onError(error) {  
  88.   alert('code: '    + error.code    + '\n' +  
  89.    'message: ' + error.message + '\n');  
  90.  }  
  91.  
  92. </script> 
  93. </head> 
  94. <body> 
  95.  <p id="geolocation">Finding geolocation...</p> 
  96.  <button onclick="clearWatch();">Clear Watch</button> 
  97. </body> 
  98. </html> 
  99. geolocationSuccess   

当得到一个有效地理位置信息时,此用户回调函数被调当获得一个地理位置信息时,此用户回调函数被调用。

  1. function(position) {    
  2.     // 进行处理     
  3. }    
  4. function(position) {  
  5.  // 进行处理   

参数:

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. }    
  11. function geolocationSuccess(position) {  
  12.  alert('Latitude: '          + position.coords.latitude          + '\n' +  
  13.   'Longitude: '         + position.coords.longitude         + '\n' +  
  14.   'Altitude: '          + position.coords.altitude          + '\n' +  
  15.   'Accuracy: '          + position.coords.accuracy          + '\n' +  
  16.   'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +  
  17.   'Heading: '           + position.coords.heading           + '\n' +  
  18.   'Speed: '             + position.coords.speed             + '\n' +  
  19.   'Timestamp: '         + new Date(position.timestamp)      + '\n');  

geolocationError 

当geolocation函数发生错误时,此用户回调函数被调用。

  1. function(error) {    
  2.     // 处理错误    
  3. }    
  4. function(error) {  
  5.  // 处理错误  

参数:

error:设备返回的错误信息。(PositionError类型)

geolocationOptions 

用户定制地理位置检索的可选参数。

  1. { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true };    
  2. { 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 }    
  2. { 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.     
  13. // onError回调函数接收一个PositionError对象    
  14. function onError(error) {    
  15.     alert('code: '    + error.code    + '\n' +    
  16.         'message: ' + error.message + '\n');    
  17. }    
  18.     
  19. navigator.geolocation.getCurrentPosition(onSuccess, onError);    
  20. // 获取位置信息成功后调用的回调函数  
  21. var onSuccess = function(position) {  
  22.  alert('Latitude: '          + position.coords.latitude          + '\n' +  
  23.   'Longitude: '         + position.coords.longitude         + '\n' +  
  24.   'Altitude: '          + position.coords.altitude          + '\n' +  
  25.   'Accuracy: '          + position.coords.accuracy          + '\n' +  
  26.   'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +  
  27.   'Heading: '           + position.coords.heading           + '\n' +  
  28.   'Speed: '             + position.coords.speed             + '\n' +  
  29.   'Timestamp: '         + new Date(position.timestamp)      + '\n');  
  30. };  
  31.  
  32. // onError回调函数接收一个PositionError对象  
  33. function onError(error) {  
  34.  alert('code: '    + error.code    + '\n' +  
  35.   'message: ' + error.message + '\n');  
  36. }  
  37.  
  38. navigator.geolocation.getCurrentPosition(onSuccess, onError); 

完整的范例:

  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>        
  4. <title>Device Properties Example</title>    
  5.     
  6. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>    
  7. <script type="text/javascript" charset="utf-8">    
  8.     
  9.     // 等待加载PHoneGap    
  10.     document.addEventListener("deviceready", onDeviceReady, false);     
  11.         
  12.     // PhoneGap加载完毕    
  13.     function onDeviceReady() {    
  14.         navigator.geolocation.getCurrentPosition(onSuccess, onError);    
  15.     }    
  16.         
  17.     // 获取位置信息成功后调用的回调函数    
  18.     function onSuccess(position) {    
  19.         var element = document.getElementById('geolocation');    
  20.         element.innerHTML = 'Latitude: '           + position.coords.latitude              + '<br />' +    
  21.                             'Longitude: '          + position.coords.longitude             + '<br />' +    
  22.                             'Altitude: '           + position.coords.altitude              + '<br />' +    
  23.                             'Accuracy: '           + position.coords.accuracy              + '<br />' +    
  24.                             'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +    
  25.                             'Heading: '            + position.coords.heading               + '<br />' +    
  26.                             'Speed: '              + position.coords.speed                 + '<br />' +    
  27.                             'Timestamp: '          + new Date(position.timestamp)          + '<br />';    
  28.     }    
  29.         
  30.     // onError回调函数接收一个PositionError对象    
  31.     function onError(error) {    
  32.         alert('code: '    + error.code    + '\n' +    
  33.             'message: ' + error.message + '\n');    
  34.     }    
  35.     
  36. </script>    
  37. </head>    
  38. <body>    
  39.     <p id="geolocation">Finding geolocation...</p>    
  40. </body>    
  41. </html>    
  42. <!DOCTYPE html> 
  43. <html> 
  44. <head>   
  45. <title>Device Properties Example</title> 
  46.  
  47. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
  48. <script type="text/javascript" charset="utf-8"> 
  49.  
  50.  // 等待加载PHoneGap  
  51.  document.addEventListener("deviceready", onDeviceReady, false);   
  52.    
  53.  // PhoneGap加载完毕  
  54.  function onDeviceReady() {  
  55.   navigator.geolocation.getCurrentPosition(onSuccess, onError);  
  56.  }  
  57.    
  58.  // 获取位置信息成功后调用的回调函数  
  59.  function onSuccess(position) {  
  60.   var element = document.getElementById('geolocation');  
  61.   element.innerHTML = 'Latitude: '           + position.coords.latitude              + '<br />' +  
  62.        'Longitude: '          + position.coords.longitude             + '<br />' +  
  63.        'Altitude: '           + position.coords.altitude              + '<br />' +  
  64.        'Accuracy: '           + position.coords.accuracy              + '<br />' +  
  65.        'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +  
  66.        'Heading: '            + position.coords.heading               + '<br />' +  
  67.        'Speed: '              + position.coords.speed                 + '<br />' +  
  68.        'Timestamp: '          + new Date(position.timestamp)          + '<br />';  
  69.  }  
  70.    
  71.  // onError回调函数接收一个PositionError对象  
  72.  function onError(error) {  
  73.   alert('code: '    + error.code    + '\n' +  
  74.    'message: ' + error.message + '\n');  
  75.  }  
  76.  
  77. </script> 
  78. </head> 
  79. <body> 
  80.  <p id="geolocation">Finding geolocation...</p> 
  81. </body> 
  82. </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. };    
  6. var onSuccess = function(position) {  
  7.  alert('Latitude: '  + position.coords.latitude             + '\n' +  
  8.   'Longitude: ' + position.coords.longitude            + '\n' +  
  9.   'Timestamp: ' + new Date(position.timestamp * 1000)  + '\n');  
  10. }; 

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. // 获取位置信息出错后调用的回调函数    
  14. var onError = function() {    
  15.    alert('onError!');    
  16. };    
  17.     
  18. navigator.geolocation.getCurrentPosition(onSuccess, onError);    
  19. // 获取位置信息成功后调用的回调函数  
  20. var onSuccess = function(position) {  
  21.  alert('Latitude: '          + position.coords.latitude          + '\n' +  
  22.   'Longitude: '         + position.coords.longitude         + '\n' +  
  23.   'Altitude: '          + position.coords.altitude          + '\n' +  
  24.   'Accuracy: '          + position.coords.accuracy          + '\n' +  
  25.   'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +  
  26.   'Heading: '           + position.coords.heading           + '\n' +  
  27.   'Speed: '             + position.coords.speed             + '\n' +  
  28.   'Timestamp: '         + new Date(position.timestamp)      + '\n');  
  29. };  
  30.  
  31. // 获取位置信息出错后调用的回调函数  
  32. var onError = function() {  
  33.    alert('onError!');  
  34. };  
  35.  
  36. navigator.geolocation.getCurrentPosition(onSuccess, onError); 

完整的范例:

  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>        
  4. <title>Geolocation Position Example</title>    
  5.     
  6. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>    
  7. <script type="text/javascript" charset="utf-8">    
  8.     
  9.     // 设置一个当PhoneGap加载完毕后触发的事件    
  10.     document.addEventListener("deviceready", onDeviceReady, false);    
  11.         
  12.     // PhoneGap加载完毕并就绪    
  13.     function onDeviceReady() {    
  14.         navigator.geolocation.getCurrentPosition(onSuccess, onError);    
  15.     }    
  16.         
  17.     // 显示位置信息中的“Position”属性    
  18.     function onSuccess(position) {    
  19.         var div = document.getElementById('myDiv');    
  20.         
  21.         div.innerHTML = 'Latitude: '             + position.coords.latitude  + '<br/>' +    
  22.                         'Longitude: '            + position.coords.longitude + '<br/>' +    
  23.                         'Altitude: '             + position.coords.altitude  + '<br/>' +    
  24.                         'Accuracy: '             + position.coords.accuracy  + '<br/>' +    
  25.                         'Altitude Accuracy: '    + position.coords.altitudeAccuracy  + '<br/>' +    
  26.                         'Heading: '              + position.coords.heading   + '<br/>' +    
  27.                         'Speed: '                + position.coords.speed     + '<br/>';    
  28.     }    
  29.         
  30.     // 如果获取位置信息出现问题,则显示一个警告    
  31.     function onError() {    
  32.         alert('onError!');    
  33.     }    
  34.     
  35. </script>    
  36. </head>    
  37. <body>    
  38.     <div id="myDiv"></div></body>    
  39. </html>    
  40. <!DOCTYPE html> 
  41. <html> 
  42. <head>   
  43. <title>Geolocation Position Example</title> 
  44.  
  45. <script type="text/javascript" charset="utf-8" src="phonegap.js"></script> 
  46. <script type="text/javascript" charset="utf-8"> 
  47.  
  48.  // 设置一个当PhoneGap加载完毕后触发的事件  
  49.  document.addEventListener("deviceready", onDeviceReady, false);  
  50.    
  51.  // PhoneGap加载完毕并就绪  
  52.  function onDeviceReady() {  
  53.   navigator.geolocation.getCurrentPosition(onSuccess, onError);  
  54.  }  
  55.    
  56.  // 显示位置信息中的“Position”属性  
  57.  function onSuccess(position) {  
  58.   var div = document.getElementById('myDiv');  
  59.    
  60.   div.innerHTML = 'Latitude: '             + position.coords.latitude  + '<br/>' +  
  61.       'Longitude: '            + position.coords.longitude + '<br/>' +  
  62.       'Altitude: '             + position.coords.altitude  + '<br/>' +  
  63.       'Accuracy: '             + position.coords.accuracy  + '<br/>' +  
  64.       'Altitude Accuracy: '    + position.coords.altitudeAccuracy  + '<br/>' +  
  65.       'Heading: '              + position.coords.heading   + '<br/>' +  
  66.       'Speed: '                + position.coords.speed     + '<br/>';  
  67.  }  
  68.    
  69.  // 如果获取位置信息出现问题,则显示一个警告  
  70.  function onError() {  
  71.   alert('onError!');  
  72.  }  
  73.  
  74. </script> 
  75. </head> 
  76. <body> 
  77.  <div id="myDiv"></div></body> 
  78. </html> 

Android的特异情况:

altitudeAccuracy: Android设备上不支持该属性,返回值总是null。

小结:PhoneGap API帮助文档翻译Geolocation地理位置的内容介绍完了,希望通过PhoneGap API文档内容的学习能对你有所帮助!

责任编辑:zhaolei 来源: 网络转载
相关推荐

2011-09-13 14:40:16

PhoneGap AP

2011-09-13 13:47:56

PhoneGap AP

2011-09-13 16:08:58

PhoneGap AP

2011-09-13 10:17:26

PhoneGap AP

2011-09-13 10:40:25

PhoneGap AP

2011-09-13 11:06:08

PhoneGap AP

2011-09-13 14:07:45

PhoneGap AP

2011-09-13 15:51:11

PhoneGap AP

2011-12-22 09:27:36

PhoneGap APGeolocation

2012-09-04 10:15:00

IBMdw

2011-11-18 09:28:17

地理位置API

2014-03-20 10:50:44

HTML5 定位技术

2011-09-13 13:17:27

PhoneGap AP

2023-10-27 16:12:29

2009-11-20 09:25:50

TwitterAPI

2013-11-15 15:46:16

Nagios地图

2013-07-16 11:16:03

云计算

2012-02-14 16:51:07

HTML 5

2012-02-26 21:41:46

手机黑客

2011-06-30 09:07:22

Gartner云计算
点赞
收藏

51CTO技术栈公众号