Nicolas Le Bot
Nicolas Le Bot

Reputation: 324

Geolocation doesn't work with cordova

I'm currently working on a mobile application with Intel XDK (In background it's Cordova finally, that's why I put Cordova in title.)

With an Ajax request, I get some adresses and with these adresses I want to calculate the distance between them and the current position of user.

So, I get adresses, I convert them and I make the difference.

But actually, nothing is working !

function codeAddress(id, addresse) {
    geocoder.geocode( { 'address': addresse}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
            setTimeout(function(){}, 100);
        }

        console.log(id);
        console.log(addresse);

        //document.addEventListener("intel.xdk.device.ready",function(){
        if (navigator.geolocation)
        {
            if (status == google.maps.GeocoderStatus.OK)
            {
                navigator.geolocation.getCurrentPosition(function(position) {
                    addressEvent = results[0].geometry.location;
                    var pos = {
                        lat: position.coords.latitude,
                        lng: position.coords.longitude
                    };
                    var position = new google.maps.LatLng(pos.lat, pos.lng)
                    var resultat = google.maps.geometry.spherical.computeDistanceBetween(addressEvent, position);
                    console.log(resultat);
                    console.log(addressEvent);
                    console.log(pos);
                    console.log(position);
                    var convert = Math.floor(resultat);
                    var finalConvert = convert + " m";
                    var distance = document.createElement('span');
                    distance.innerHTML = finalConvert;
                    distance.className = "geo";
                    document.getElementsByClassName('meta-info-geo')[id].appendChild(distance);
                }, function() {
                    handleLocationError(true, infoWindow);
                });
            }
        }
        //},false);
    });
}

In the console.log(id), console.log(addresse), I HAVE results !

Actually i'm getting 4 IDs and 4 adresses.

I checked on all the topics I could find on StackOverFlow, and I had normally to add the line in // with the addEventListener but it changes nothing.

Is there someone who knows how to change that ?

ps : Of course, cordova geoloc is in the build and permissions are granted !

EDIT : I'm targeting Android 4.0 min and iOS 5.1.1. I'm using SDK.

EDIT 2 : enter image description here

Upvotes: 0

Views: 756

Answers (1)

xmnboy
xmnboy

Reputation: 2310

Geolocation frequently does not work the way people expect it to work, for a variety of reasons that have been expressed here and here.

You can experiment with geo by using the "Hello, Cordova" sample app that is in the XDK and also available on GitHub. Try using it on a variety of devices to see how things work. Push the "fine" button to initiate a single geo call for a "fine" location and push the "coarse" button to initiate a single geo call for a "coarse" location. Push the "watch" button to initiate a request for a series of geo data points (set to coarse or fine by pushing one of the single buttons first).

The behavior you get in the Emulate tab will be dramatically different than what you get on a real device. The type of device (Android, iOS, etc.) and the version of that device will influence your results; the manufacturer of the device and your location (inside or outside) will influence your results. Do not assume that making a call to the geo APIs will always give you immediate and reliable data, geolocation hardware does not work that way... In fact, you cannot assume that you can even get a valid result! See the two links I pointed to earlier in the post for some reasons why.

Upvotes: 1

Related Questions