No return of geolocation using Phonegap

Asked

Viewed 1,299 times

11

I’m using the code to pick up the geolocation of the Phonegap, but even so, when I emulo the app, it returns me nothing. There is something wrong in the code?

index.html

<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <title>Hello World</title>

    <script type="text/javascript" charset="utf-8">
    document.addEventListener("deviceready", onDeviceReady, false);

    var watchID = null;
    function onDeviceReady() {
        var options = { enableHighAccuracy: true };
        watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
    }

    function onSuccess(position) {
        var element = document.getElementById('geolocation');
        element.innerHTML = 'Latitude: '  + position.coords.latitude      + '<br />' +
                            'Longitude: ' + position.coords.longitude     + '<br />' +
                            '<hr />'      + element.innerHTML;
    }

    // clear the watch that was started earlier
    //
    function clearWatch() {
        if (watchID != null) {
            navigator.geolocation.clearWatch(watchID);
            watchID = null;
        }
    }

        function onError(error) {
          alert('code: '    + error.code    + '\n' +
                'message: ' + error.message + '\n');
        }

    </script>
  </head>
  <body>
    <p id="geolocation">Watching geolocation...</p>
        <button onclick="clearWatch();">Clear Watch</button>
  </body>
 </html>

config.xml

<feature name="Geolocation">
    <param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
</feature>
<feature name="NetworkStatus">
    <param name="android-package" value="org.apache.cordova.networkinformation.NetworkManager" />
</feature>

Androidmanifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  • 1

    Using the emulator, you also need to simulate the location by DDMS if you are using Eclipse. This has already been done?

  • I configured the AVD following the steps described in this link Android Developer. I set up the hw.gps

  • 3

    Okay, you set up the emulator to simulate the existence of a GPS, but that alone doesn’t tell you the coordinates. To do this, open the DDMS perspective and with the emulator started, select the Emulator Control tab and in the Location Controls section you can manually enter a latitude and longitude to send, then yes your simulated GPS will go into action.

  • Thank you very much. I will configure here and try to simulate the GPS. If I can’t, I will inform you =D

  • 1

    Hello @Luannaiozzi if you managed to find a solution please post it in the response field for other people with the same problem as you be benefited ;) Thank you..

  • 1

    So, I set up the DDMS on my machine and it didn’t work, but another person who is working with me on this project, set it up and worked normally. I believe it’s my eclipse settings.

Show 1 more comment

1 answer

1

Add this to your Androidmanifest.xml

 <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

And then try to simulate your locations within the app itself and then in the DDMS to see if it might be a Phonegap bug or constraint.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.