简体   繁体   中英

Phonegap geoLocation not working on Android

So i am trying to make geoLocation work on android. I have used the code below and i have to say that in the browser it works perfectly, however when i run this on a device it doesn't work. This is my code :

Index.html

<p id="lat">Stad :</p>
<p id="lng">Straat :</p>

<script>
    window.onload = function(){
        document.addEventListener("deviceready", init, false);
    }

    function init(){
        navigator.geolocation.getCurrentPosition(positionSucces, positionError);
    }

    function positionSucces(position){
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;

        document.getElementById("lat").innerHTML = latitude;
        document.getElementById("lng").innerHTML = longitude;
    }

    function positionError(error){
        alert(error.message);
    }

</script>

Does somebody have hints on how to make this work for mobile devices? (certainly android)

How are you testing on an android device? PhoneGap Desktop? Or are you building your app (Adobe PhoneGap Build) and then testing on the device?

  1. Is that all the code that you have? I am surprised it works on browser because, deviceready event does not fire on browser. You need cordova to fire this event.
  2. (If you are testing on PhoneGap Desktop)Make sure you have turned on your location on your device, because "If Geolocation service is turned off the onError callback is invoked after timeout interval (if specified). If timeout parameter is not specified (as in your case) then no callback is called." Source - https://github.com/apache/cordova-plugin-geolocation
  3. (If you are using PhoneGap build) You said, you have plugin included in your config.xml. If you are using Adobe PhoneGap build... make sure that file (config.xml) is in the same hierarchy(directory) as your index.html. PhoneGap by default creates config.xml a directory above index.html.

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