how to get current location in phonegap?

核能气质少年 提交于 2019-12-01 06:22:12

问题


i added Geolocation plugin in my app and used this code for getting the current location in phonegap. but this code does not work.

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

function onDeviceReady() {
    alert("4");
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
}

function onSuccess(position) {
    alert("1");
}

function onError(error) {
    alert("3");
}

Where is the problem in my code?

thank you and sorry for my poor english.


回答1:


  1. check if geolocation is properly installed or not

  2. restart your phone (silly solution but it works sometimes.I faced it once)

  3. check if your geo permissions & settings are turned on in device

  4. test with this code

    options = { enableHighAccuracy: true }; navigator.geolocation.getCurrentPosition(onSuccess, onError, options);

  5. If you're using android, try to get the error with eclipse logcat.




回答2:


On iOS this will always work one way or the other. If the plugin fails, the browser's own Geolocation API will be used. There's some undesired side effects with the fallback though:

  • The permission dialog that pop's up has the html file's name in the title instead of the app name
  • Granted permissions will only persist as long as the app is running

On Android it only works if you have set the correct permissions in app/AndroidManifest.xml:

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


来源:https://stackoverflow.com/questions/25010097/how-to-get-current-location-in-phonegap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!