【发布时间】:2018-06-01 04:22:35
【问题描述】:
我是 phonegap 的新手,它是插件。 我正在使用 cordova-plugin-mauron85-background-geolocation 插件,我的问题是我无法将 latitude 或 longitude 显示为警报。我无法弄清楚哪个变量正在存储这些数据。如果有人可以帮助我并向我解释哪个变量具有纬度或经度,我会很感激,这样我就可以在对话框中发出警报。 这是我正在使用的代码。
<div>GPS</div>
<button onclick="gps()" id="st">gps</button><br>
<button onclick="pos()" id="st3">my position</button><br>
<button onclick="gps_end()" id="st2">gps end</button><br>
<button onclick="stats()" id="st5">status</button><br>`
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript">`
function onDeviceReady() {
BackgroundGeolocation.configure({
locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
desiredAccuracy: BackgroundGeolocation.HIGH_ACCURACY,
stationaryRadius: 50,
distanceFilter: 50,
notificationTitle: 'Background tracking',
notificationText: 'enabled',
debug: true,
interval: 10000,
fastestInterval: 5000,
activitiesInterval: 10000,
url: 'xxxxx',
httpHeaders: {
'X-FOO': 'bar'
},
// customize post properties
postTemplate: {
lat: '@latitude',
lon: '@longitude',
foo: 'bar' // you can also add your own properties
}
});
BackgroundGeolocation.on('location', function(location) {
// handle your locations here
// to perform long running operation on iOS
// you need to create background task
BackgroundGeolocation.startTask(function(taskKey) {
// execute long running task
// eg. ajax post location
// IMPORTANT: task has to be ended by endTask
BackgroundGeolocation.endTask(taskKey);
});
});
BackgroundGeolocation.on('stationary', function(stationaryLocation) {
// handle stationary locations here
});
BackgroundGeolocation.on('error', function(error) {
console.log('[ERROR] BackgroundGeolocation error:', error.code,
error.message);
});
BackgroundGeolocation.on('start', function() {
console.log('[INFO] BackgroundGeolocation service has been started');
});
BackgroundGeolocation.on('stop', function() {
console.log('[INFO] BackgroundGeolocation service has been stopped');
});
BackgroundGeolocation.on('authorization', function(status) {
console.log('[INFO] BackgroundGeolocation authorization status: ' + status);
if (status !== BackgroundGeolocation.AUTHORIZED) {
setTimeout(function() {
var showSettings = confirm('App requires location tracking permission.
Would you like to open app settings?');
if (showSetting) {
return BackgroundGeolocation.showAppSettings();
}
}, 1000);
}
});
BackgroundGeolocation.on('background', function() {
console.log('[INFO] App is in background');
// you can also reconfigure service (changes will be applied immediately)
BackgroundGeolocation.configure({ debug: true });
});
BackgroundGeolocation.on('foreground', function() {
console.log('[INFO] App is in foreground');
BackgroundGeolocation.configure({ debug: false });
});
BackgroundGeolocation.checkStatus(function(status) {
console.log('[INFO] BackgroundGeolocation service is running',
status.isRunning);
console.log('[INFO] BackgroundGeolocation services enabled',
status.locationServicesEnabled);
console.log('[INFO] BackgroundGeolocation auth status: ' +
status.authorization);
alert("entre a la funcion checkstatus")
// you don't need to check status before start (this is just the example)
if (!status.isRunning) {
BackgroundGeolocation.start(); //triggers start on start event
}
});
}
document.addEventListener('deviceready', onDeviceReady, false);
function gps(){
alert("Starts GPS")
BackgroundGeolocation.start();
BackgroundGeolocation.checkStatus();
alert ("GPS ON")
}
function gps_end(){
BackgroundGeolocation.checkStatus();
alert("Ending gps")
BackgroundGeolocation.stop();
alert("No Gps")
}
function pos() {
alert("start pos function")
BackgroundGeolocation.getCurrentLocation(
function (locations) {
alert(locations.latitude);
});
}
function stats() {
BackgroundGeolocation.checkStatus();
var watchID = BackgroundGeolocation.getCurrentLocation(onSuccess,
onError, options);
function onSuccess(position) {
BackgroundGeolocation.start();
lat=position.coords.latitude;
long=position.coords.longitude;
alert(long)
}
}
function onError(position){
show_alert('GPS','Unable to load GPS data.'+position.message,'OK');
}
</script>
</body>
【问题讨论】:
标签: cordova plugins geolocation phonegap