Android Web 应用程序:应用程序没有足够的地理定位权限

Android web application: Application does not have sufficient geolocation permissions

我正在尝试制作一个 Android 仅在 WebView 组件内显示地图的应用程序。

我已经从 How to Convert a Website into an Android Application using Android Studio 教程开始,然后尝试让浏览器使用 JavaScript 和设备的位置。

为了允许 WebView 使用我应用了以下指南和答案的位置

但是,当我点击当前位置按钮时,我仍然收到以下消息:

Geolocation error: Application does not have sufficient geolocation permissions..

我不认为这是一个重复的问题,因为这是关于 Leafet map JS 库和特定案例的具体问题。

我试过的

目前我的应用程序由以下文件组成:

MainActivity.java

package com.vinaysomawat.careerhigh;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;



public class MainActivity extends ActionBarActivity {
    //private WebView mywebview;


    public class GeoWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // When user clicks a hyperlink, load in the existing WebView
            view.loadUrl(url);
            return true;
        }
    }

    //public class GeoWebChromeClient extends WebChromeClient {
    public class GeoWebChromeClient extends android.webkit.WebChromeClient {
        @Override
        public void onGeolocationPermissionsShowPrompt(String origin,
                                                       //GeolocationPermissions.Callback callback) {
                                                       android.webkit.GeolocationPermissions.Callback callback){
            // Always grant permission since the app itself requires location
            // permission and the user has therefore already granted it
            callback.invoke(origin, true, false);
        }
    }



    WebView mywebview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mywebview = (WebView)findViewById(R.id.webView);
        WebSettings webSettings = mywebview.getSettings();

        mywebview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        mywebview.getSettings().setBuiltInZoomControls(true);
        mywebview.setWebViewClient(new GeoWebViewClient());
        // Below required for geolocation
        mywebview.getSettings().setJavaScriptEnabled(true);
        mywebview.getSettings().setGeolocationEnabled(true);
        mywebview.setWebChromeClient(new GeoWebChromeClient());

        mywebview.getSettings().setAppCacheEnabled(true);
        mywebview.getSettings().setDatabaseEnabled(true);
        mywebview.getSettings().setDomStorageEnabled(true);

        mywebview.getSettings().setGeolocationDatabasePath(getFilesDir().getPath());


        mywebview.loadUrl("https://domoritz.github.io/leaflet-locatecontrol/demo/");

    }





    @Override
    public void onBackPressed(){
        if(mywebview.canGoBack()) {
            mywebview.goBack();
        } else
        {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int id = item.getItemId();

        if(id == R.id.action_settings){
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vinaysomawat.careerhigh"
    android:installLocation="auto">

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/faviconcircular"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/faviconcircular"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".SplashActivity" android:theme="@style/SplashTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity android:name=".MainActivity" />
    </application>

</manifest>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/webView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

如何解决地理定位问题?谢谢指教。

根据您的权限将此添加到您的 class,然后在 onCreate

中调用它
    private fun checkPermissions() {
    val permission = ActivityCompat.checkSelfPermission(
        this@MainActivity,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
    )
    if (permission != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
            this@MainActivity,
            permissions,
            requestExternalStorageCode
        )
    }
}

另外不要忘记覆盖权限回调以检查用户是否授予您权限:

    override fun onRequestPermissionsResult(
    requestCode: Int,
    permissions: Array<out String>,
    grantResults: IntArray
) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults)

    if (requestCode == requestExternalStorageCode) {
        if (grantResults.size > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            //to whatever you need after the permission is granted
        } else {
            finish()
        }
    }
}