Cordova地理定位在安卓设备上无法使用

3
我想在安卓设备上使用地理定位功能,我用Apache Cordova写了一个应用程序。但无论是在安卓电脑模拟器还是在安卓手机上,地理定位都无法正常工作。
我尝试了以下链接:http://cordova.apache.org/docs/en/2.4.0/cordova_geolocation_geolocation.md.html#geolocation.getCurrentPosition_full_examplehttp://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation 我的 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>

我有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' ]
$ 

我在互联网上(包括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模拟器中运行应用程序并运行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的文档?而且你也没有说出你遇到了什么问题(控制台中是否有错误?只是无法获取位置?) - QuickFix
我发送了错误的链接。我尝试使用3.3.0版本的代码(与2.4版本的代码相同)http://goo.gl/8Z4NmH。 我没有看到任何错误或调试消息。我的应用程序在屏幕上写下“查找位置...”,然后什么也没有发生。 我在我的应用程序中添加了“console.log('Test log');”,但在“adb logcat”输出中我没有看到它。 - martin
你找到解决这个问题的方法了吗?我也遇到了同样的问题。 - mlakhara
2个回答

4

嗯,我为这个问题苦苦挣扎了几个小时(也许是几天),最终找到了以下链接; https://issues.apache.org/jira/browse/CB-5977

我不确定它是否已经或将要过时。但无论如何,我的解决方案是 "rm org.apache.cordova.geolocation" ,因为它根本不起作用。

然后我找到了mozilla的以下链接;https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/Using_geolocation

我所做的唯一一件事就是获取用户位置的以下内容;

<!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>

注意:它在phonegapDevApp无线上工作,但在真实设备上没有工作。这就是为什么我不断寻找并痛苦地学习,发现js文件应该在body而不是header中,并将我的navigator.geolocation.getPosition命令从“initialize”替换到“initialize”之前。然后再次添加geolocation插件!
是的,最终,我的应用程序完美地工作了。它显示了地图和我的当前位置在启动应用程序时固定。
希望对你也有用!

1
geo_options.timeout 设置为适当的值解决了我的问题。感谢分享。 - Stefan

0

我之前在安卓上使用cordova-plugin-geolocation的最新版本时遇到了问题。 请尝试以下版本

<plugin name="cordova-plugin-geolocation" spec="2.4.3" />

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接