【问题标题】:Gelocation not working in Android - PhoneGap Build地理定位在 Android 中不起作用 - PhoneGap Build
【发布时间】:2016-12-30 06:39:26
【问题描述】:

我正在制作一个简单的 PhoneGap 应用,它可以获取用户的位置。

用于我的构建的代码来自地图示例documentation

以下是我的 xml 代码:

<content src="index.html" />
<preference name="DisallowOverscroll" value="true" />
<preference name="android-minSdkVersion" value="14" />
<preference name="StatusBarOverlaysWebView" value="false" />
<preference name="StatusBarBackgroundColor" value="#ee6e73" />
<preference name="StatusBarStyle" value="blacktranslucent" />
<preference name="orientation" value="portrait" />
<plugin name="cordova-plugin-statusbar" source="npm" spec="~2.1.3" />
<plugin name="cordova-plugin-geolocation" source="npm" />
<plugin name="org.apache.cordova.globalization" source="npm" />
<plugin name="org.apache.cordova.geolocation" source="npm" />
<plugin name="cordova-plugin-console" source="npm" spec="~1.0.3" />
<plugin name="cordova-plugin-dialogs" source="npm" spec="~1.2.1" />
<plugin name="cordova-plugin-whitelist" source="npm" spec="~1.2.1" />

<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
</feature>
<allow-navigation href="*" />
<access origin="*" launch-external="yes" />
<access origin="*://*.googleapis.com/*" subdomains="true" />
<access origin="*://*.gstatic.com/*" subdomains="true" />
<access origin="*://*.google.com/*" subdomains="true" />
<access origin="*://*.googleusercontent.com/*" subdomains="true" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>

虽然该应用在浏览器上运行良好,但 android 版本不会加载该位置。

进一步注意到 maps.gstatic.com 和 mts.googleapis.com 没有从应用加载,两者都加载到浏览器中,然后导致提供位置

提前致谢。

更新 1:

为了避免混淆,我使用了phonegap桌面应用程序生成的默认config.xml,仍然没有获取到位置。

<?xml version='1.0' encoding='utf-8'?>
<widget id="com.phonegap.helloworld" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<name>new</name>
<description>
Hello World sample application that responds to the deviceready event.
</description>
<author email="support@phonegap.com" href="http://phonegap.com">
    PhoneGap Team
</author>
<content src="index.html" />
<preference name="DisallowOverscroll" value="true" />
<preference name="android-minSdkVersion" value="14" />
<plugin name="cordova-plugin-battery-status" source="npm" spec="~1.1.1" />
<plugin name="cordova-plugin-camera" source="npm" spec="~2.1.1" />
<plugin name="cordova-plugin-media-capture" source="npm" spec="~1.2.0" />
<plugin name="cordova-plugin-console" source="npm" spec="~1.0.2" />
<plugin name="cordova-plugin-contacts" source="npm" spec="~2.0.1" />
<plugin name="cordova-plugin-device" source="npm" spec="~1.1.1" />
<plugin name="cordova-plugin-device-motion" source="npm" spec="~1.2.0" />
<plugin name="cordova-plugin-device-orientation" source="npm" spec="~1.0.2" />
<plugin name="cordova-plugin-dialogs" source="npm" spec="~1.2.0" />
<plugin name="cordova-plugin-file" source="npm" spec="~4.1.1" />
<plugin name="cordova-plugin-file-transfer" source="npm" spec="~1.5.0" />
<plugin name="cordova-plugin-geolocation" source="npm" spec="~2.1.0" />
<plugin name="cordova-plugin-globalization" source="npm" spec="~1.0.3" />
<plugin name="cordova-plugin-inappbrowser" source="npm" spec="~1.3.0" />
<plugin name="cordova-plugin-media" source="npm" spec="~2.2.0" />
<plugin name="cordova-plugin-network-information" source="npm" spec="~1.2.0" />
<plugin name="cordova-plugin-splashscreen" source="npm" spec="~3.2.1" />
<plugin name="cordova-plugin-statusbar" source="npm" spec="~2.1.2" />
<plugin name="cordova-plugin-vibration" source="npm" spec="~2.1.0" />
<plugin name="cordova-plugin-whitelist" source="npm" spec="~1.2.1" />

<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
    <allow-intent href="market:*" />
</platform>
<platform name="ios">
    <allow-intent href="itms:*" />
    <allow-intent href="itms-apps:*" />
</platform>

【问题讨论】:

    标签: android cordova google-maps geolocation phonegap-build


    【解决方案1】:

    试试这个插件从安卓设备获取lat long,然后在插件调用成功时调用map

    Cordova Plugin Geolocation

    将经纬度添加到成功函数中接收到的地图

    document.addEventListener("deviceready", appReady, false);
    function appReady(){
        navigator.geolocation.getCurrentPosition(onSuccess, onError);
    }   
    
    // onSuccess Callback
    // This method accepts a Position object, which contains the
    // current GPS coordinates
    
    var onSuccess = function(position) {
        alert('Latitude: '          + position.coords.latitude          + '\n' +
              'Longitude: '         + position.coords.longitude         + '\n' +
              'Altitude: '          + position.coords.altitude          + '\n' +
              'Accuracy: '          + position.coords.accuracy          + '\n' +
              'Altitude Accuracy: ' + position.coords.altitudeAccuracy  + '\n' +
              'Heading: '           + position.coords.heading           + '\n' +
              'Speed: '             + position.coords.speed             + '\n' +
              'Timestamp: '         + position.timestamp                + '\n');
    };
    
    // onError Callback receives a PositionError object
    
    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }
    

    试试这个,让我知道这是否适合你。

    【讨论】:

    • 谢谢,但插件已经包含在我的代码中
    • 您是否已将cordova.js 添加到您的.html 文件中,或者如果您正在通过PhoneGap Build Online 构建应用程序,则添加phonegap.js 也尝试在deviceready event listner 上调用此函数
    • 也试过这个,但没有运气。我没有物理设备,会不会是模拟器的问题?
    • Here 是一些模拟器工作地理定位插件的修复。
    • 谢谢,但这也无济于事
    【解决方案2】:

    我已将我的构建更新为

     <preference name="phonegap-version" value="cli-6.3.0" />
    

    你必须删除

    <plugin name="cordova-plugin-geolocation" source="npm" />
    <plugin name="org.apache.cordova.geolocation" source="npm" />
    

    <feature name="Geolocation">
    <param name="android-package" value="org.apache.cordova.geolocation.GeoBroker" />
    </feature>
    

    从您的代码中添加以下代码

    安卓版

    <plugin name="cordova-plugin-geolocation"  spec="0.3.12" source="npm"/>
    <plugin name="cordova-plugin-network-information"  spec="0.2.15" source="npm" />
    

    IOS

    <plugin name="cordova-plugin-geolocation"  spec="https://github.com/apache/cordova-plugin-geolocation.git"/>
     <plugin name="cordova-plugin-network-information"  spec="https://github.com/apache/cordova-plugin-network-information.git" />
    

    并使用它来加载位置

     navigator.geolocation.getCurrentPosition(zoomToLocation, locationError, { maximumAge: 5000, timeout: 7500 });
    
     function zoomToLocation(position) {
                var geolocate = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    

    【讨论】:

    • 谢谢,但结果没有变化它仍然没有得到地理位置
    • @meetrk85 : 我正在从 Navigator 加载位置
    猜你喜欢
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    相关资源
    最近更新 更多