【发布时间】:2014-02-17 02:35:07
【问题描述】:
我想在 Android 上使用地理定位。我用 Apache Cordova 编写应用程序。 地理定位不适用于安卓电脑模拟器和安卓手机。
我试试http://cordova.apache.org/docs/en/2.4.0/cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition_full_example 和http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation
我的平台/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>
我有 cordova 3.3.1 和这些插件:
$ 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' ]
$
我在 Internet 上发现了很多问题(在 stackoverflow 上也是如此),但都没有用 :-(
你知道我哪里出错了吗?
更新:
现在我在 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>
在 Android 模拟器中运行 app 并运行 logcat 后,我看到:
$ 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
你能帮帮我吗?
【问题讨论】:
-
如果您使用的是cordova 3.3,为什么还要查看cordova 2.4 的文档?而且你也没有说你有什么问题(控制台中的错误?只是没有得到位置?)
-
我发送了错误的链接。我尝试 3.3.0 版本的代码(代码与 2.4 相同)goo.gl/8Z4NmH。我没有看到任何错误或调试消息。我的应用程序将“正在查找位置...”写入屏幕,然后什么也没有发生。我添加“console.log('Test log');”到我的应用程序,但在“adb logcat”输出中我看不到它。
-
你有没有找到解决这个问题的办法。我也遇到同样的问题。
标签: android cordova geolocation cordova-3