我是phonegap的新手,也是插件。 我使用的是cordova-plugin-mauron85-background-geolocation插件,我的问题是我不能将其显示为纬度或经度的警报。我无法弄清楚哪个变量存储了这些数据。如果有人可以帮助我并解释我的变量具有纬度或经度,那么我会在一个对话框中提醒我。 这是我使用的代码。
<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>
答案 0 :(得分:0)
配置已更新,可以更快地锁定位置。
改变这一点,看看发生了什么:
BackgroundGeolocation.on('location', function(location) {
alert("Current location: " + JSON.stringify(location));
});
当然,只要您获得任何位置即可。你应该至少得到一个。然后你可能需要移动足够的距离才能获得新的距离。
您是否将<plugin name="cordova-plugin-mauron85-background-geolocation"/>
添加到config.xml或从命令行安装了插件?
如果您在完全安装应用后没有获得有关权限的任何问题,那可能就是没有获取任何位置的原因。
另外,看看是否有任何JavaScript错误。
如果您还没有,可以在GitHub上报告问题。
这是我目前使用的配置:
var options = {
locationProvider: BackgroundGeolocation.ACTIVITY_PROVIDER,
desiredAccuracy: BackgroundGeolocation.MEDIUM_ACCURACY),
stationaryRadius: 25,
distanceFilter: 20,
notificationTitle: 'Tracking position in the background',
notificationText: 'enabled',
debug: false,
interval: 10000,
fastestInterval: 5000,
activitiesInterval: 5000,
stopOnTerminate: false,
startOnBoot: false,
startForeground: true,
pauseLocationUpdates: false,
stopOnStillActivity: true,
saveBatteryOnBackground: false
这在config.xml中:
<plugin name="cordova-plugin-mauron85-background-geolocation">
<variable name="GOOGLE_PLAY_SERVICES_VERSION" value="11+" />
<variable name="ANDROID_SUPPORT_LIBRARY_VERSION" value="23+" />
<variable name="ALWAYS_USAGE_DESCRIPTION" value="The app needs background location tracking" />
<variable name="MOTION_USAGE_DESCRIPTION" value="Accelerometer use increases battery efficiency by intelligently toggling location tracking" />
</plugin>
这就是我在位置事件中提取的内容:
var timestamp = position.time;
var latitude = position.latitude;
var longitude = position.longitude;
var accuracy = position.accuracy;
var altitude = position.altitude;
var speed = position.speed;
var heading = position.bearing;