13 Replies Latest reply on Aug 21, 2016 9:46 AM by neilm64907889

    PhoneGap Build - Android Geolocation not working

    neilm64907889 Level 1

      So I have geolocation working perfectly on IOS - gives me coordinates. When I run it on Android, it doesnt even give me an alert as per the code below. Im using CLI-6.3.0 and my config.xml is an follows:

       

      <plugin name="cordova-plugin-geolocation" source="npm" />

       

       

       

       

       

      function getLocationfromGoogle() {

          navigator.geolocation.getCurrentPosition(

            function(pos) {

            //  $("#lat_field").val(pos.coords.latitude);

            //  $("#long_field").val(pos.coords.longitude);

             

               localStorage.latitude = pos.coords.latitude ;

        localStorage.longitude = pos.coords.longitude ;

        //localStorage.altitude = pos.coords.altitude;

        localStorage.timestampgps = Date(pos.timestamp);

       

              alert(localStorage.latitude);

              alert(localStorage.longitude);

              //alert(localStorage.altitude);

              alert(localStorage.timestampgps);

             

              var geocoder = new google.maps.Geocoder();

              var latLng = new google.maps.LatLng(pos.coords.latitude,pos.coords.longitude);

                    geocoder.geocode({ 'latLng': latLng}, function (results, status) {

                    if (status == google.maps.GeocoderStatus.OK) {

                      //console.log(results[0].formatted_address);

                     // $("#adresim").val(results[0].formatted_address);

                      alert(results[0].formatted_address);

                     

                      }

                      else {

                  alert('Google convertion is not succesfully done.'); 

       

       

                  }

                  });

            },function error(msg){

       

       

                      alert('Please exit this screen, enable your GPS location on your phone and restart the process.'); 

       

       

            },{maximumAge:600000, timeout:5000, enableHighAccuracy: true}

       

       

          );

          }

       

       

      Thanks in advance

        • 1. Re: PhoneGap Build - Android Geolocation not working
          VectorP Level 4

          You just posted the function definition, not the call of the function and the status of the script flow.

          Questions:

          - did you include a reference to cordova.js, without actually including such file in your assets?

          - did you wait for the deviceready event to fire before attempting to get any geolocation coordinates?

          - did the function actually get called and executed?

          • 2. Re: PhoneGap Build - Android Geolocation not working
            neilm64907889 Level 1

            You just posted the function definition, not the call of the function and the status of the script flow.

            Questions:

            - did you include a reference to cordova.js, without actually including such file in your assets?

            I referenced it, and its included

             

            - did you wait for the deviceready event to fire before attempting to get any geolocation coordinates?

            Yes

            - did the function actually get called and executed?

            Yes, remember my post says it works perfect on IOS, and it works fine in a browser. 

             

            It almost sounds like an Android specific , perhaps permission related

            What I did notice online is the following to a similar problem, but adding this to config.xml gives me errors on build.

             

                <gap:config-file overwrite="true" parent="/manifest" platform="android">

                    <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"/>

                </gap:config-file>

             

             

             

            1 person found this helpful
            • 3. Re: PhoneGap Build - Android Geolocation not working
              VectorP Level 4

              I referenced it, and its included

              It should not be included in your assets that you send to PGB, since Phonegap will 'inject' a proper version.

               

              - did the function actually get called and executed?

              Yes, remember my post says it works perfect on IOS, and it works fine in a browser. 

              I read that. But we're talking Android, now. Did the function actually get called and executed there?

               

              You wrote in your original question:

              When I run it on Android, it doesnt even give me an alert as per the code below.

              Now, there are several alerts in your code. If none of them shows, then you must first find out whether or not that part of your code is reached at all, beginning with the first line of the function.

              So, insert an alert prior to navigator.geolocation.getCurrentPosition(...). Also, display the object type value of 'navigator.geolocation' and 'navigator.geolocation.getCurrentPosition'.

               

              My guess: it only works nicely in iOS and the browser, because it's not using the Geolocation plugin there (but rather the HTML5 Geolocation API, which has a similar interface). If that guess is correct, it would be helpful to see your full config.xml. Also, please confirm that you have both config.xml and index.html in the root directory f your zip file.

              • 4. Re: PhoneGap Build - Android Geolocation not working
                neilm64907889 Level 1

                Firstly thanks for the help.

                 

                I just added an alert prior as you suggested , using Android and im now getting an error message: PositionError:PERMISSION_DENIED, illegal access

                • 5. Re: PhoneGap Build - Android Geolocation not working
                  neilm64907889 Level 1

                  config.xml:

                   

                  <?xml version   ="1.0" encoding="UTF-8" ?>

                  <widget xmlns   = "http://www.w3.org/ns/widgets"

                      xmlns:gap   = "http://phonegap.com/ns/1.0"

                      id          = "com.blah.blah"

                      versionCode = "1003"

                      version     = "1.00b">

                     

                     

                      <name>blah</name>

                     

                         <description>

                          blah

                      </description>

                   

                   

                     

                      <author href="http://www.blah.com">

                          blah

                      </author>

                     

                      <content src="index.html"/>

                      <icon src="images/icon2.png" width="256" height="256" />

                     

                   

                    <preference name="phonegap-version" value="cli-6.3.0" />-->

                                         

                       <!--   

                    <preference name="phonegap-version" value="cli-5.1.1" />

                   

                    <preference name="phonegap-version" value="3.7.0" /> -->

                    

                   

                   

                    <preference name="stay-in-webview" value="false" />

                   

                   

                  <access origin="*" />

                   

                   

                  <!--NEW -->

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

                  <gap:plugin name="org.apache.cordova.device" source="npm"/>

                  <gap:plugin name="org.apache.cordova.camera" source="npm"/>

                  <gap:plugin name="org.apache.cordova.device-orientation" source="npm"/>

                  <gap:plugin name="org.apache.cordova.file" source="npm"/>

                  <gap:plugin name="org.apache.cordova.file-transfer" source="npm"/>

                  <!--<gap:plugin name="org.apache.cordova.geolocation" source="npm"/> -->

                  <gap:plugin name="org.apache.cordova.globalization" source="npm" />

                   

                   

                     

                  <plugin name="cordova-plugin-geolocation" source="npm" />

                   

                   

                   

                   

                  <gap:plugin name="org.apache.cordova.inappbrowser" source="npm"/>

                  <gap:plugin name="org.apache.cordova.network-information" source="npm"/>

                  <gap:plugin name="org.apache.cordova.dialogs" source="npm"/>

                  <gap:plugin name="org.apache.cordova.splashscreen" source="npm"/>

                  <!--<gap:plugin name="org.apache.cordova.core.geolocation" />-->

                  <gap:plugin name="org.apache.cordova.media" source="npm"/>

                  <gap:plugin name="org.apache.cordova.media-capture" source="npm"/>

                   

                   

                   

                   

                   

                   

                   

                   

                     

                      <preference name="orientation" value="portrait" />

                      <preference name="android-minSdkVersion" value="9" />

                   

                    <gap:platform name="ios" />

                    <gap:platform name="android" />

                  <!-- <gap:platform name="webos" />

                    <gap:platform name="symbian" />

                    <gap:platform name="blackberry" />

                    <gap:platform name="winphone" />-->

                   

                   

                   

                   

                   

                    <icon src="images/icon2.png" />

                   

                   

                   

                   

                  <feature name="Geolocation">

                      <param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />

                  </feature>

                   

                   

                   

                   

                   

                   

                    <!--ANDROID-->

                   

                   

                    <gap:splash src="splash/android/ldpi.png" gap:platform="android" gap:density="ldpi" />

                    <gap:splash src="splash/android/mdpi.png" gap:platform="android" gap:density="mdpi" />

                    <gap:splash src="splash/android/hdpi.png" gap:platform="android" gap:density="hdpi" />

                    <gap:splash src="splash/android/xhdpi.png" gap:platform="android" gap:density="xhdpi" />

                   

                   

                   

                   

                   

                   

                   

                  </widget>

                  • 6. Re: PhoneGap Build - Android Geolocation not working
                    VectorP Level 4

                    This can't be your latest config. It doesn't validate as well-formed xml, since it contains an unmatched end-of-comment-string, here:

                    <preference name="phonegap-version" value="cli-6.3.0" />-->

                     

                    Also, you are using a bunch of old, deprecated plugins from the pgb repo. Replace them with the latest versions of the plugins from npm. While doing so, you may as well drop the gap: namespace.

                     

                    You have

                    preference name="android-minSdkVersion" value="9"

                    but the lowest value supported by PGB is 14.

                     

                    You have two 'default icons'. Remove the first and rename the second, while keeping in mind that it should be located in the root and called 'icon.png'

                     

                    You have a feature element for Geolocation. Remove it, because it shouldn't be there.

                     

                    You're missing the larger splashes for Android, and there are none of the required splashes for iOS.

                    • 7. Re: PhoneGap Build - Android Geolocation not working
                      neilm64907889 Level 1

                      My bad, I was cleaning it up before I posed the config.xml, hence the --> at the end of the CLI-6.3.0

                       

                      Just with regards to the permission errors I am getting on geolocation, is there not a permission of sort I need to add to config.xml?

                      • 8. Re: PhoneGap Build - Android Geolocation not working
                        VectorP Level 4

                        No need to add permissions. The plugin will take care of that.

                        • 9. Re: PhoneGap Build - Android Geolocation not working
                          neilm64907889 Level 1

                          Im absolutely stumped, ive made the changes as you have said, but I still get the permission issue, what am I doing wrong?

                          • 10. Re: PhoneGap Build - Android Geolocation not working
                            VectorP Level 4

                            What is it that is throwing the error?

                            - the Android OS?

                            - the device?

                            - the app's code?

                            - the plugin?

                            - something else?

                             

                            And is it really exactly written this way?:

                            PositionError:PERMISSION_DENIED, illegal access

                            (with PositionError in camelCase, Permission denied in caps and with underscore (which is really strange for an error message), and 'illegal access' without any clue what is being accessed?)

                            • 11. Re: PhoneGap Build - Android Geolocation not working
                              neilm64907889 Level 1

                              I added  the following alerts to the beginning of the function, the bold section below gives me as per attached screen shot on Android

                              device.

                               

                                function getLocationfromGoogle() {

                                     

                                      alert("We are now firing getLocationfromGoogle");

                                

                                      alert(navigator.geolocation);

                                     alert(navigator.geolocation.getCurrentPosition);

                                 

                                      navigator.geolocation.getCurrentPosition(

                                    function(pos) {

                                  

                                     

                                       localStorage.latitude = pos.coords.latitude ;

                                localStorage.longitude = pos.coords.longitude ;

                                //localStorage.altitude = pos.coords.altitude;

                                localStorage.timestampgps = Date(pos.timestamp);

                               

                                      alert(localStorage.latitude);

                                      alert(localStorage.longitude);

                                      //alert(localStorage.altitude);

                                      alert(localStorage.timestampgps);

                                     

                                     

                                     

                                      var geocoder = new google.maps.Geocoder();

                                      var latLng = new google.maps.LatLng(pos.coords.latitude,pos.coords.longitude);

                                            geocoder.geocode({ 'latLng': latLng}, function (results, status) {

                                            if (status == google.maps.GeocoderStatus.OK) {

                                              //console.log(results[0].formatted_address);

                                             // $("#adresim").val(results[0].formatted_address);

                                              alert(results[0].formatted_address);

                                             

                                              }

                                              else {

                                          alert('Google convertion is not succesfully done.'); 

                               

                               

                                          }

                                          });

                                    },function error(msg){

                               

                               

                                              alert('Please exit this screen, enable your GPS location on your phone and restart the process.'); 

                               

                               

                                    },{maximumAge:600000, timeout:5000, enableHighAccuracy: true}

                               

                               

                                  );

                                  }

                               

                              IMG_3355.JPG

                              • 12. Re: PhoneGap Build - Android Geolocation not working
                                VectorP Level 4

                                - So, what are the GPS/Location settings for your Android test device?

                                - What happens on your other Android test devices?

                                • 13. Re: PhoneGap Build - Android Geolocation not working
                                  neilm64907889 Level 1

                                  Holy crap, now im a little embarrassed, I removed the SIM and replaced with a spare - works 100's - i think the data is depleted.

                                   

                                  Sheeesh thanks VectorP for your high level of patience for n00bs like myself.