问题
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