Apache Cordova GeoLocation not providing data

霸气de小男生 提交于 2019-12-11 04:19:02

问题


I'm using Apache Cordova to get the lat and long coordinates of a device to pass on to a parse database. However, the coordinates are returning blank and no error alert appears. JSFiddle shows no syntax errors.

document.addEventListener("deviceready", onDeviceReady, false);
var lat1=""; var long1="";

// Cordova is ready
function onDeviceReady() {
    navigator.geolocation.getCurrentPosition(onSuccess, onError, options);
 alert("Device is ready!");
}

// onSuccess Geolocation
function onSuccess(position) {
    long1=position.coords.longitude;
    lat1=position.coords.latitude;
}

// onError Callback receives a PositionError object
function onError(error) {
    alert("unable to get location");
}

function options(){ enableHighAccuracy: true }

回答1:


It seems you made this inverted:

Change:

position.coords.longitude=long1;
position.coords.latitude=lat1;

To:

long1 = position.coords.longitude;
lat1 = position.coords.latitude;



回答2:


I had the same problem. I fixed it by explicitly installing the plugin: cordova plugin add cordova-plugin-geolocation

However, I'm using $cordovaGeolocation.getCurrentPosition, not navigator.geolocation.getCurrentPosition.

Hope this help you! :)



来源:https://stackoverflow.com/questions/21356589/apache-cordova-geolocation-not-providing-data

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