在 phonegap 上使用 cordova-plugin-mauron85-background-geolocation 显示纬度和经度
Posted
技术标签:
【中文标题】在 phonegap 上使用 cordova-plugin-mauron85-background-geolocation 显示纬度和经度【英文标题】:Display Latitude and Longitude with cordova-plugin-mauron85-background-geolocation on phonegap 【发布时间】: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>
【问题讨论】:
【参考方案1】:已更新配置以更快地锁定位置。
改成这个,看看发生了什么:
BackgroundGeolocation.on('location', function(location)
alert("Current location: " + JSON.stringify(location));
);
这当然是在您获得任何位置的情况下。你应该至少得到一个。那么你可能需要移动足够的距离才能获得新的。
您是在 config.xml 中添加<plugin name="cordova-plugin-mauron85-background-geolocation"/>
还是从命令行安装插件?
如果您在全新安装应用后未收到任何有关权限的问题,这可能是未获取任何位置的原因。
另外,看看您是否收到任何 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;
【讨论】:
感谢您的回答。我现在就试试,然后告诉你。以上是关于在 phonegap 上使用 cordova-plugin-mauron85-background-geolocation 显示纬度和经度的主要内容,如果未能解决你的问题,请参考以下文章