问题描述
我想在Android上使用地理位置。我在Apache Cordova上编写应用程序。
在andoid pc模拟器和oroid手机上,地理定位不起作用。
我尝试
和
我的平台/ android / AndroidManifest.xml:
<?xml version ='1.0'encoding ='utf-8'?>
< manifest android:hardwareAccelerated =trueandroid:versionCode =1android:versionName =0.0.1android:windowSoftInputMode =adjustPanpackage =net.example.testxmlns:android = http://schemas.android.com/apk/res/android\">
支持屏幕android:anyDensity =trueandroid:largeScreens =trueandroid:normalScreens =trueandroid:resizeable =trueandroid:smallScreens =trueandroid:xlargeScreens =true />
< uses-permission android:name =android.permission.INTERNET/>
< uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_LOCATION_EXTRA_COMMANDS/>
< uses-sdk android:minSdkVersion =10android:targetSdkVersion =19/>
< application android:debuggable =trueandroid:hardwareAccelerated =trueandroid:icon =@ drawable / iconandroid:label =@ string / app_name>
< intent-filter>
< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< / application>
< / manifest>
www / config.xml:
<?xml version ='1.0'encoding ='utf-8'?>
< widget id =net.example.testversion =0.0.1xmlns =http://www.w3.org/ns/widgetsxmlns:cdv =http:// cordova .apache.org / NS / 1.0\" >
<名称>测试< /名称>
< description>
响应deviceready事件的示例Apache Cordova应用程序。
< / description>
< author email =dev@cordova.apache.orghref =http://cordova.io>
Apache Cordova团队
< / author>
< content src =index.html/>
< access origin =*/>
< feature name =Geolocation>
< param name =android-packagevalue =org.apache.cordova.GeoBroker/>
< / feature>
< / widget>
我有cordova 3.3.1和这些插件:
$ cordova -v
3.3.1-0.4.2
$ cordova插件ls
['org.apache.cordova.console' ,
'org.apache.cordova.device',
'org.apache.cordova.dialogs',
'org.apache.cordova.geolocation']
$
我在互联网上发现了很多问题(也是在stackoverflow上),但都不起作用:-(
你有什么想法,我有什么错误?
更新:
现在我已经在index.html中:
<!DOCTYPE html>
< html>< head>
< script type =text / javascriptcharset =utf-8src =cordova.js> < / script>
< script type =text / javascriptcharset =utf-8>
//等待设备API库加载
文档.addEventListener(deviceready,onDeviceReady,false);
//设备API s可用
函数onDeviceReady(){
console.log('in onDeviceReady()');
navigator.geolocation.getCurrentPosition(onSuccess,onError);
}
// onSuccess地理定位
函数onSuccess(position){
console.log('onSuccess()');
console.log(position.coords.latitude);
console.log(position.coords.longitude);
var element = document.getElementById('geolocation');
element.innerHTML ='Latitude:'+ position.coords.latitude +'< br />'+
'经度:'+ position.coords.longitude +'< br /> '+
'高度:'+ position.coords.altitude +'< br />'+
'准确度:'+ position.coords.accuracy +'< br />'+
'高度准确度:'+ position.coords.altitudeAccuracy +'< br />'+
'标题:'+ position.coords.heading +'< br />'+
'速度:'+ position.coords.speed +'< br />'+
'时间戳:'+ position.timestamp +'< br />';
}
$ b $ // onError回调接收到一个PositionError对象
函数onError(error){
console.log('inErError()');
console.log(error.code);
console.log(error.message);
alert('code:'+ error.code +'\\\
'+
'message:'+ error.message +'\\\
');
}
< / script>
< / head>< body>< p id =geolocation>寻找地理位置...< / p>< / body>< / html>
在Android模拟器中运行应用程序并运行logcat后,我看到:
$ telnet localhost 5554
尝试127.0.0.1 ...
连接到本地主机。
转义字符是'^]'。
Android控制台:输入'help'获取命令列表
OK
geo fix 50.60 10.20
OK
$ adb logcat Cordova:* DroidGap: * CordovaLog:* *:S
- 等待设备 -
I / CordovaLog(805):将日志级别更改为DEBUG(3)
I / CordovaLog(805):找到起始页位置:index.html
I / CordovaLog(805):将日志级别更改为DEBUG(3)
I / CordovaLog(805):找到起始页位置:index.html
D / CordovaLog 805):file:///android_asset/www/index.html:第11行:在onDeviceReady()
D / CordovaLog(805):file:///android_asset/www/index.html:第33行:在onError()
D / CordovaLog(805):file:///android_asset/www/index.html:Line 34:null
你能帮我吗?
好的,我努力解决这个问题几小时(也许几天),并找出以下链接;
我不确定它是否已经或将被弃用。
但无论如何,我的解决方案是rm org.apache.cordova.geolocation,因为它根本没有工作。
然后我遇到了mozilla的以下链接;
我为了获得用户的位置而做的唯一事情如下:
<!DOCTYPE html>
< html>
< head>
< title>设备属性示例< / title>
< / head>
< body>
< script src =http://192.168.43.15:8081/target/target-script-min.js#anonymous>< / script>
< script type =text / javascriptcharset =utf-8src =cordova.js>< / script>
< script type =text / javascriptcharset =utf-8>
函数geo_success(position){
alert(success+:+ position.coords.latitude +,position.coords.longitude);
//do_something(position.coords.latitude,position.coords.longitude);
函数geo_error(){
alert(Sorry,no position available。);
}
var geo_options = {
enableHighAccuracy:true,
maximumAge:30000,
timeout:27000
};
var wpid = navigator.geolocation.getPosition(geo_success,geo_error,geo_options);
< / script>
< p id =geolocation>寻找地理位置...< / p>
< / body>
< / html>
注意:它在phonegapDevApp wirelessy上工作,但在真实设备上无法使用。这就是为什么我一直在痛苦地看着和学习js文件应该在体内而不是标题,并将navigator.geolocation.getPosition命令从初始化替换为初始化之上。然后再次添加地理位置插件!
最后,我的应用程序完美运行。它显示了我的当前位置的地图固定在我的应用程序启动。
希望它也适用于您!
I want to use geolocation on Android. I write app in Apache Cordova.Geolocation do not work both on andoid pc emulator and andoid phone.
I try http://cordova.apache.org/docs/en/2.4.0/cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition_full_exampleand http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation
My platforms/android/AndroidManifest.xml:
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" android:windowSoftInputMode="adjustPan" package="net.example.test" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
<application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="Test" android:theme="@android:style/Theme.Black.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
www/config.xml:
<?xml version='1.0' encoding='utf-8'?>
<widget id="net.example.test" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>Test</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<access origin="*" />
<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.GeoBroker" />
</feature>
</widget>
I have cordova 3.3.1 and these plugins:
$ cordova -v
3.3.1-0.4.2
$ cordova plugin ls
[ 'org.apache.cordova.console',
'org.apache.cordova.device',
'org.apache.cordova.dialogs',
'org.apache.cordova.geolocation' ]
$
I find a lot of question on the Internet (on stackoverflow too), but all did not work :-(
Do you have any idea, where I have an error?
Update:
Now I have in index.html:
<!DOCTYPE html>
<html><head>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
function onDeviceReady() {
console.log('in onDeviceReady()');
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
// onSuccess Geolocation
function onSuccess(position) {
console.log('in onSuccess()');
console.log(position.coords.latitude);
console.log(position.coords.longitude);
var element = document.getElementById('geolocation');
element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
'Longitude: ' + position.coords.longitude + '<br />' +
'Altitude: ' + position.coords.altitude + '<br />' +
'Accuracy: ' + position.coords.accuracy + '<br />' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '<br />' +
'Heading: ' + position.coords.heading + '<br />' +
'Speed: ' + position.coords.speed + '<br />' +
'Timestamp: ' + position.timestamp + '<br />';
}
// onError Callback receives a PositionError object
function onError(error) {
console.log('in onError()');
console.log(error.code);
console.log(error.message);
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
</script>
</head><body><p id="geolocation">Finding geolocation...</p></body></html>
After I run app in Android emulator and run logcat, I see:
$ telnet localhost 5554
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Android Console: type 'help' for a list of commands
OK
geo fix 50.60 10.20
OK
$ adb logcat Cordova:* DroidGap:* CordovaLog:* *:S
- waiting for device -
I/CordovaLog( 805): Changing log level to DEBUG(3)
I/CordovaLog( 805): Found start page location: index.html
I/CordovaLog( 805): Changing log level to DEBUG(3)
I/CordovaLog( 805): Found start page location: index.html
D/CordovaLog( 805): file:///android_asset/www/index.html: Line 11 : in onDeviceReady()
D/CordovaLog( 805): file:///android_asset/www/index.html: Line 33 : in onError()
D/CordovaLog( 805): file:///android_asset/www/index.html: Line 34 : null
Can you please help me?
Well , I struggled with that problem for hours (maybe days) and figured out the following link ;https://issues.apache.org/jira/browse/CB-5977
I am not sure if it is already or going to be deprecated though.But anyways, my solution was to "rm org.apache.cordova.geolocation " because it was not working at all.
Then I came across with the following link of mozilla ;https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation
and the only thing that I was doing in order to get the location of the user is the following;
<!DOCTYPE html>
<html>
<head>
<title>Device Properties Example</title>
</head>
<body>
<script src="http://192.168.43.15:8081/target/target-script-min.js#anonymous"></script>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
function geo_success(position) {
alert("success" + " : " + position.coords.latitude + ", position.coords.longitude");
//do_something(position.coords.latitude, position.coords.longitude);
}
function geo_error() {
alert("Sorry, no position available.");
}
var geo_options = {
enableHighAccuracy: true,
maximumAge : 30000,
timeout : 27000
};
var wpid = navigator.geolocation.getPosition(geo_success, geo_error, geo_options);
</script>
<p id="geolocation">Finding geolocationsss...</p>
</body>
</html>
NOTE : It did work on phonegapDevApp wirelessy but did not work on real device . That's why I kept looking and learnt in a painful way that js files should be inside body not header , and replaced my navigator.geolocation.getPosition command from "initialize" to above "initialize" . And then added the geolocation plugin again !
Yes finally, my app worked perfectly. It showed the map with my current location pinned in the start up of my app.
Hopefully it works for you too !
这篇关于科尔多瓦地理定位不适用于Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!