【问题标题】:Android web application: Application does not have sufficient geolocation permissionsAndroid Web 应用程序:应用程序没有足够的地理定位权限
【发布时间】:2022-01-07 00:12:49
【问题描述】:

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

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

为了让 WebView 使用该位置,我应用了以下指南和答案

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

地理位置错误:应用程序没有足够的地理位置 权限..

我不认为这是一个重复的问题,因为这是关于 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>

如何解决地理定位问题?谢谢你的建议。

【问题讨论】:

标签: android android-studio webview leaflet geolocation


【解决方案1】:

使用您的权限将其添加到您的班级,然后在 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()
        }
    }
}

【讨论】:

  • 你的意思是ACCESS_FINE_LOCATION而不是WRITE_EXTERNAL_STORAGE
  • 是的,添加 ACCESS_FINE_LOCATION。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 2018-07-18
  • 2012-04-04
  • 2019-07-10
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多