【问题标题】:PhoneGap GeoLocation var typesPhoneGap GeoLocation 变量类型
【发布时间】:2013-04-23 15:11:08
【问题描述】:
//   This method accepts a `Position` object, which contains
//   the current GPS coordinates
function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: '               + position.coords.latitude                                 + '<br />' +
                            'Longitude: '          + position.coords.longitude             + '<br />' +
                            'Altitude: '           + position.coords.altitude              + '<br />' +
                            'Accuracy: '           + position.coords.accuracy              + '<br />' +
                            'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +
                            'Heading: '            + position.coords.heading               + '<br />' +
                            'Speed: '              + position.coords.speed                 + '<br />' +
                            'Timestamp: '          +                                         position.timestamp             + '<br />';
}

这是从 phonegap 自己的网站上获取的关于 geoLocation 的示例,但有一些问题我不能安静地理解,我们使用位置变量对象来访问坐标的成员,如纬度,经度但是我们如何在 js 中创建位置对象,即使有不是变量类型。

【问题讨论】:

    标签: javascript android html cordova geolocation


    【解决方案1】:

    “位置”变量由 Phonegap 创建,并在您执行以下操作时传递给回调成功函数:

    navigator.geolocation.getCurrentPosition(onSuccess, onError);
    

    onSuccess 函数:

    function onSuccess(position) {
        //Process the position here
    }
    

    Phonegap api 文档中的完整示例:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Exemple Geolocation</title>
    
        <script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>
        <script type="text/javascript" charset="utf-8">
    
        // Attendre que PhoneGap soit prêt
        //
        document.addEventListener("deviceready", onDeviceReady, false);
    
        // PhoneGap est prêt
        //
        function onDeviceReady() {
            navigator.geolocation.getCurrentPosition(onSuccess, onError);
        }
    
        // Fonction de callback onSuccess, reçoit un objet Position
        //
        function onSuccess(position) {
            var element = document.getElementById('geolocation');
            element.innerHTML = 'Latitude : '                + position.coords.latitude          + '<br/>' +
                                'Longitude : '               + position.coords.longitude         + '<br/>' +
                                'Altitude : '                + position.coords.altitude          + '<br/>' +
                                'Précision : '               + position.coords.accuracy          + '<br/>' +
                                'Précision de l'altitude : ' + position.coords.altitudeAccuracy  + '<br/>' +
                                'Direction : '               + position.coords.heading           + '<br/>' +
                                'Vitesse : '                 + position.coords.speed             + '<br/>';
        }
    
        // Fonction de callback onError, reçoit un objet PositionError
        //
        function onError(error) {
            alert('code : '    + error.code    + '\n' +
                  'message : ' + error.message + '\n');
        }
    
        </script>
      </head>
      <body>
        <p id="geolocation">Recherche de géolocalisation...</p>
      </body>
    </html>
    

    希望这会有所帮助!再见!

    【讨论】:

    • 没问题:)!不要忘记将此答案标记为解决问题。
    • 你知道我该怎么做吗?
    • 通过单击答案左侧的复选框轮廓将其标记为已接受的答案。请参阅:FAQ
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多