Phonegap 3.0 获取当前地址位置

新版本的cordova 3.0 中,使用官方的示例可直接获取当前手机的地理位置,前提是手机开启了gps,或可联网。

获取到的是经纬度坐标值等信息,可通过google api 实现通过经纬度获取当前地理位置名称。

google api 地址:http://maps.google.com/maps/api/geocode/json?latlng=经度,纬度&language=zh-CN&sensor=true

phonegap 代码:
<!DOCTYPE html>
<html>
  <head>
    <title>Device Properties Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova.js"></script><!--cordova 3.0+ -->
    <script type="text/javascript" charset="utf-8">

    // Wait for device API libraries to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // device APIs are available
    //
    function onDeviceReady() {
        navigator.geolocation.getCurrentPosition(onSuccess, onError);
    }

    // onSuccess Geolocation
    //
    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 />';
    }

        // onError Callback receives a PositionError object
        //
        function onError(error) {
            alert('code: '    + error.code    + '\n' +
                  'message: ' + error.message + '\n');
        }

    </script>
  </head>
  <body>
    <p id="geolocation">Finding geolocation...</p>
  </body>
</html>
posted @ 2013-09-24 14:15  zuike  阅读(4240)  评论(2编辑  收藏  举报