简体   繁体   English

Phonegap geoLocation在Android上不起作用

[英]Phonegap geoLocation not working on Android

So i am trying to make geoLocation work on android. 所以我试图让geoLocation在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 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) (当然是android)

How are you testing on an android device? 您如何在Android设备上进行测试? PhoneGap Desktop? PhoneGap桌面? Or are you building your app (Adobe PhoneGap Build) and then testing on the device? 还是要构建您的应用程序(Adobe PhoneGap Build),然后在设备上进行测试?

  1. Is that all the code that you have? 这就是您拥有的所有代码吗? I am surprised it works on browser because, deviceready event does not fire on browser. 我很惊讶它在浏览器上有效,因为deviceready事件不会在浏览器上触发。 You need cordova to fire this event. 您需要cordova才能触发此事件。
  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." (如果正在PhoneGap Desktop上进行测试)请确保已打开设备上的位置,因为“如果关闭了地理定位服务,则在超时间隔(如果指定)后调用onError回调。如果未指定timeout参数(如在您的情况下),则不会调用任何回调。” Source - https://github.com/apache/cordova-plugin-geolocation 来源-https: //github.com/apache/cordova-plugin-geolocation
  3. (If you are using PhoneGap build) You said, you have plugin included in your config.xml. (如果您正在使用PhoneGap构建)您说过,您的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. 如果您正在使用Adobe PhoneGap build ...,请确保文件(config.xml)与index.html位于同一层次结构(目录)中。 PhoneGap by default creates config.xml a directory above index.html. PhoneGap默认情况下在index.html上方的目录中创建config.xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM