简体   繁体   中英

phonegap geolocation doesn't work

went according to this guide Phonegap Geoloactaion and got this code eventually(android):

<html>
<head>
<title>Device Properties Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.1.1.min.js"></script>
<script type="text/javascript" charset="utf-8">


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

// Cordova is ready
//
function onDeviceReady() {
jQuery(window).ready(function(){
    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>

and it's stuck on the "Finding geolocation..." message. usually when apps want to use my location they ask to turn on GPS and you can see a little icon on the notification bar that clarifies that it turned on and searching for GPS but this doesn't seem to happen to my code. EDIT: I tried the geolocation.watchPosition Full Example and because it has a timeout it alerts me with a timeout, so I guess he tries to find my coords but fails too, Why is that? Maps and other nav app work fine

Did you put

<feature name="http://api.phonegap.com/1.0/geolocation" />

on your config.xml file?

for more information on config.xml file in PhoneGap check this page

Are you testing on your Emulator ?

If yes, then you need to push the coordinates manually by going to Eclipse -> DDMS -> Emulator Control and send the location coordinates to the Emulator from there.

Also check if the Settings on your device have allowed the use of Location services.

我有这个问题。有一天它工作,第二天起它停止工作。当重新启动手机时,它工作

When GPS works

In phonegap the application wont ask you to turn on gps, you have to implement this manually. Besides you can provide some options, eg

var config = {
   enableHighAccuracy: true,
   timeout: 20000,
   maximumAge: 18000000
}

So you error callback will now be triggered on timeout and when geolocation cannot determine your position. You should just debug your problem and see what the error callback returns.

 navigator.geolocation.getCurrentPosition(onSuccess, onError, config);

When it does not work

In your case gps won't be fired. I cannot say really much regarding this problem because you only provided html5 code. One thing I could imagine is that you are missing the cordova.js file.

For better testing and debugging use Ripple emulator https://chrome.google.com/webstore/detail/ripple-emulator-beta/geelfhphabnejjhdalkjhgipohgpdnoc

It emulates webapps and even has a geolocation mocker. This way you can debug your problem better because you can view the web console.

I know this is old, but I have noticed that for GPS to work in phoneGap Apps, you need to "agree" to share your location with Google.

When I have disagreed before, the location would get timed out everytime.

As you are loading jQuery remotely; can you verify whether it's loaded? For example, if you put an alert() in the onload handler, does it show up?

function onDeviceReady() {
  jQuery(window).ready(function(){
    alert("jQuery ready");
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
  });
}

If you don't see an alert message, you might have to check your 'res/xml/config.xml' to see if the 'access origin' is set up correctly (eg using a wildcard)

<access origin="*" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM