开发者

Call phonegap API from external page

I have a basic Android app that links to a few external webpages. I have placed phonegap.js on those pages and am trying to get to the GPS info but don't get anything. Basically, the app is set up to use a webview to load the external page. The external page simply开发者_如何学编程 has the sample code from the phonegap site but I don't get any GPS info. How can I get to the phonegap API if both the js and html page are on an external server?

<!DOCTYPE html>
 <html>
  <head>
    <title>Device Properties Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
    <script type="text/javascript" charset="utf-8">
    // Wait for PhoneGap to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    //
    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: '          + new Date(position.timestamp)          + '<br />';
}

// onError Callback receives a PositionError object

function onError(error) {
    alert("error");
}

    </script>
  </head>
  <body>
    <p id="geolocation">Finding my geolocation...</p>
  </body>
</html>


It does not matter whether the HTML/JS is on an external server or embedded within the app. In both cases the JS code is going to execute in the Mobile browser on the device. Please explain the exact error that you are getting.

Make sure your native app has the permission to access location : uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"

In case you are trying on Android simulator, please refer to the following Android quirk :

Android Quirks : The Android 2.x simulators will not return a geolocation result unless the enableHighAccuracy option is set to true.

{ enableHighAccuracy: true }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜