[英]Latitude Longitude tracking in Android Phonegap?
I am new to android platform so I wanted to start my learning with existing sample code. 我是android平台的新手,所以我想从现有的示例代码开始学习。 I am trying to acquire GPS Coordinates so i wote the below code. 我试图获取GPS坐标,所以我写了下面的代码。 In the below code once the device is started onDeviceReady() method is firing but onSuccess() method is not firing. 在下面的代码中,一旦启动设备,就会触发onDeviceReady()方法,但不会触发onSuccess()方法。 How to solve this issue? 如何解决这个问题? I am using cordova-2.8.0.jar in my project. 我在我的项目中使用cordova-2.8.0.jar。
<!DOCTYPE html>
<html xml:lang="en" lang="en">
<head>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;">
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
var watchID = null;
// Cordova is ready
//
function onDeviceReady() {
// Throw an error if no update is received every 30 seconds
alert("Test1");
var options = {
timeout: 3000
};
alert("TEst2"+options);
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
alert("TEst3");
}
// onSuccess Geolocation
//
function onSuccess(position) {
alert("RAmesh");
/* var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'<hr />' + element.innerHTML;*/
}
// onError Callback receives a PositionError object
//
function onError(error) {
/* alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');*/
}
</script>
</head>
<body>
<p id="geolocation">Finding geolocation...</p>
</body>
</html>
Try changing your options as follows: 尝试如下更改选项:
var options = {
enableHighAccuracy: true,
timeout: 30000,
maximumAge: 0
};
Ensure you have set permission to use location in AndroidManifest.xml: 确保您已设置权限使用AndroidManifest.xml中的位置:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
and in your config.xml: 并在您的config.xml中:
<plugin name="Geolocation" value="org.apache.cordova.GeoBroker" />
First, try the instruction of Dpa99c. 首先,尝试Dpa99c的说明。
Second, use GenyMotion Emulator. 其次,使用GenyMotion模拟器。 It's a awsome emulator for Android : faster than default and you have some widget to modify the location, camera and much more. 这是Android的绝佳模拟器:比默认模拟器快,并且您有一些小部件可以修改位置,摄像头等等。
Third, I just discover one masive problem in Cordova/PhoneGap : - When you open the Android project built by cordova (in commandline), the requestUpdate is set at ..... 60 Seconds !!!!!!!!!!! 第三,我只是在Cordova / PhoneGap中发现了一个严重的问题:-当您打开由cordova构建的Android项目(在命令行中)时,requestUpdate设置为..... 60 Seconds !!!!!!!!!! ! You have a new Location every Minute ! 您每分钟都有一个新位置! Evenif you use GPS (enableHighAccuracy="true"). 即使您使用GPS(enableHighAccuracy =“ true”)。 So try to set this at 1000 where you find LocationManager.GPS_PROVIDER in org.apache.cordova.geolocation package -> GPSListener class ! 因此,尝试将其设置为1000,在org.apache.cordova.geolocation包-> GPSListener类中找到LocationManager.GPS_PROVIDER!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.