zuike
新版本的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>

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-01-22
  • 2021-11-05
  • 2021-05-27
  • 2022-12-23
  • 2022-12-23
  • 2021-12-15
猜你喜欢
  • 2021-12-21
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
  • 2021-08-13
  • 2022-01-19
相关资源
相似解决方案