1 Reply Latest reply on Feb 20, 2017 2:28 PM by kerrishotts

    Problem with Android Geolocation - PhoneGap Build

    joelb32396584

      Hello,

       

      I'm trying to update an old application I made with PhoneGap couple of years ago. In the older version everything works just fine, but now with the new version it does not send any geolocation to the server for some reason.

       

      Here is my config.xml

       

      <?xml version='1.0' encoding='utf-8'?>

      <widget id="fi.softmade.transpanel" version="1.1.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">

          <name>Transpanel</name>

          <description>

              A blank PhoneGap app.

          </description>

          <author email="support@phonegap.com" href="http://phonegap.com">

              PhoneGap Team

          </author>

          <content src="index.html" />

          <plugin name="cordova-plugin-geolocation" spec="~2.4.1" />

      <gap:plugin name="cordova-plugin-whitelist" version="1.1.0" source="npm" />

      <access origin="*" launch-external="yes" />

      <allow-intent href="http://*/*" />

      <allow-intent href="https://*/*" />

      </widget>

       

      and here is my .js

       

      // Wait for PhoneGap to load

          //

          document.addEventListener("deviceready", onDeviceReady, false);

       

       

          var watchID = null;

       

       

          // PhoneGap is ready

          //

          function onDeviceReady() {

              var options = {maximumAge:10000, enableHighAccuracy:true};

              watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);

          }

       

       

          // onSuccess Geolocation

          //

       

           var coords = {lat: "", lon: ""};

          function onSuccess(position) {

              coords.lat = position.coords.latitude;

           coords.lon = position.coords.longitude;

          }

       

       

          // onError Callback receives a PositionError object

          //

          function onError(error) {

              alert('code: '    + error.code    + '\n' +

                    'message: ' + error.message + '\n');

          }

       

       

       

       

      setInterval ( "Updateposition()", 15000 );

      function Updateposition(){

         // here you can reuse the object to send to a server

         console.log("lat: " + coords.lat);

         console.log("lon: " + coords.lon);

         var number = localStorage.getItem("number");

       

          jQuery.ajax({

                  type: "POST",

                  url:  serviceURL+"sijainti.php",

                  data: 'x='+ coords.lon+'&y='+coords.lat+'&number='+number,

                  cache: false

              });

       

      }

       

       

      var storedValue = localStorage.getItem("server");

      var serviceURL =  "http://"+ storedValue + "/www/";

      var number = localStorage.getItem("number");

        • 1. Re: Problem with Android Geolocation - PhoneGap Build
          kerrishotts Adobe Community Professional

          A lot has changed in a couple of years. I suggest reading the documentation on the current version of geolocation.

           

          You don't indicate what platforms you are testing on, or what devices you are testing on. That would be helpful. It would also be helpful to see what's in your config.xml file (minus secret/identifying information).

           

          Also don't call setInterval in this manner -- for one thing, you can watch for a position instead (can improve battery life on some platforms), and second, you shouldn't pass a string -- pass the function instead, like so:

           

          setInterval(Updateposition, 15000);

           

          And finally, don't use insecure HTTP -- use HTTPS. Certificates are free with letsencrypt.org.