Android webview 没有发送地理定位到 javascript

Android webview not sending geolocation to javascript

我正在编写一个获取用户位置并将其发送到服务器的应用程序。我在服务器端使用 JavaScript 和 PHP。我正在尝试在 android WebView 上加载此应用程序,它不会将位置发送到 JavaScript,以便可以通过 ajax 将位置发送到服务器,但是当我 运行它在我的移动设备上使用 Chrome,它工作正常。请我需要知道我做错了什么。我的 android 代码粘贴在下面

@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.setWebViewClient(new WebViewClient());
    websettings.setJavaScriptEnabled(true);
    websettings.setJavaScriptCanOpenWindowsAutomatically(true);
    websettings.setGeolocationDatabasePath( getFilesDir().getPath() );

    myWebview.setWebChromeClient(new WebChromeClient() {
        public void onGeolocationPermissionsShowPrompt(String origin, 
           GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
        }
    });

    websettings.setAppCacheEnabled(true);
    websettings.setDatabaseEnabled(true);
    websettings.setDomStorageEnabled(true);

    myWebview.loadUrl("https://example.com/proj/");
}

我已经在 android 清单文件中授予必要的权限。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

下面的代码是我的 JavaScript 处理位置

function getLocation() {

    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(reportLocation, showError);

    } else { 
      $$('locationFeedbackText').innerHTML = "Geolocation is not supported by this Devide.";
    }
  }

  function showError(error) {

  switch(error.code) {
    case error.PERMISSION_DENIED:
      $$('locationFeedbackText').innerHTML = "User denied the request for Geolocation.";
      break;
    case error.POSITION_UNAVAILABLE:
      $$('locationFeedbackText').innerHTML = "Location information is unavailable.";
      break;
    case error.TIMEOUT:
      $$('locationFeedbackText').innerHTML = "The request to get user location timed out.";
      break;
    case error.UNKNOWN_ERROR:
      $$('locationFeedbackText').innerHTML = "An unknown error occurred.";
      break;
  }
}

当我单击用户界面上的按钮时,用户位置应该被发送到服务器并显示成功消息,但显示的输出消息是来自 showError 函数“位置不可用”的消息。 请记住,当此应用程序在我的移动设备上加载 chrome 时,它会按预期工作。

试试这个 -- 权限已添加

package com.nony.myapplication;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    WebView myWebview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //added this line for permission request
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        myWebview = (WebView) findViewById(R.id.webview);
        WebSettings websettings = myWebview.getSettings();

        myWebview.setWebViewClient(new WebViewClient());
        websettings.setJavaScriptEnabled(true);
        websettings.setJavaScriptCanOpenWindowsAutomatically(true);
        websettings.setGeolocationDatabasePath(getFilesDir().getPath());

        myWebview.setWebChromeClient(new WebChromeClient() {
            public void onGeolocationPermissionsShowPrompt(String origin,
                                                           GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
            }
        });

        websettings.setAppCacheEnabled(true);
        websettings.setDatabaseEnabled(true);
        websettings.setDomStorageEnabled(true);

        myWebview.loadUrl("https://example.com/proj/");
    }


    //newly added for permission
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 1: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                return;
            }
            // other 'case' lines to check for other
            // permissions this app might request
        }
    }


    public boolean checkLocationPermission()
    {
        String permission = "android.permission.ACCESS_FINE_LOCATION";
        int res = this.checkCallingOrSelfPermission(permission);
        return (res == PackageManager.PERMISSION_GRANTED);
    }

}