首页 文章

Cordova地理定位插件不适用于Android设备

提问于
浏览
0

我'm using cordova geolocation plugin. When I run my index.html on browser I can see my location on map. But When I run my project on android device I cant get anything. I just see white page. I'米使用Android版Kitkat . 我从https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-geolocation/index.html这条链接获得指示 . 我从另一个问题尝试了一些方法 . 但它不起作用 . 我怎么解决这个问题?

<!DOCTYPE html>
<html>
<head>
    <meta name="format-detection" content="telephone=no">
    <meta name="msapplication-tap-highlight" content="no">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC-id_ZLCzQktYMWI2XrrMulXYGOzCUYhk"></script>
</head>
<body>
    <div id="map" style="width:500px;height:500px;">
    </div>
    <script>
        var Latitude = undefined;
        var Longitude = undefined;
        // Get geo coordinates
        function getMapLocation() {
            navigator.geolocation.getCurrentPosition
            (onMapSuccess, onMapError, { enableHighAccuracy: true });
        }
        // Success callback for get geo coordinates
        var onMapSuccess = function (position) {
            Latitude = position.coords.latitude;
            Longitude = position.coords.longitude;
            getMap(Latitude, Longitude);
        }
        // Get map by using coordinates
        function getMap(latitude, longitude) {
            var mapOptions = {
                center: new google.maps.LatLng(0, 0),
                zoom: 1,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map
            (document.getElementById("map"), mapOptions);
            var latLong = new google.maps.LatLng(latitude, longitude);
            var marker = new google.maps.Marker({
                position: latLong
            });
            marker.setMap(map);
            map.setZoom(15);
            map.setCenter(marker.getPosition());
        }
        // Success callback for watching your changing position
        var onMapWatchSuccess = function (position) {
            var updatedLatitude = position.coords.latitude;
            var updatedLongitude = position.coords.longitude;
            if (updatedLatitude != Latitude && updatedLongitude != Longitude) {
                Latitude = updatedLatitude;
                Longitude = updatedLongitude;
                getMap(updatedLatitude, updatedLongitude);
            }
        }
        // Error callback
        function onMapError(error) {
            console.log('code: ' + error.code + '\n' +
                'message: ' + error.message + '\n');
        }
        // Watch your changing position
        function watchMapPosition() {
            return navigator.geolocation.watchPosition
            (onMapWatchSuccess, onMapError, { enableHighAccuracy: true });
        }       
        getMapLocation();
    </script>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>      
</body>
</html>

İt is my androidmanifest.xml

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" package="com.example.geo" 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" />
<application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
        <intent-filter android:label="@string/launcher_name">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="24" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
</manifest>

1 回答

  • 0

    在googlemap api js之前添加cordova js

    <script src="cordova.js"></script>
    

    并将您的函数 getMapLocation(); 调用到 deviceReadydocumentReady 函数中

    //deviceReady
    document.addEventListener("deviceready", getMapLocation, false);
    
    
    //documentReady
    $(document).ready(function(){
        getMapLocation();
    });
    

    这会对你有所帮助 .

相关问题