使用应用中嵌入的浏览器时,无法从 Android 上的网页访问地理位置

Geo Location not accessible from a webpage on Android when using browser embedded in an app

我正在使用 navigator.geolocation.getCurrentPosition() 获取用户的当前位置。在 iOS 和 Android 上直接加载到 Web 浏览器时,这些网页上的位置功能工作正常。系统会询问用户是否允许访问位置,然后从那里开始。无论是哪个网络浏览器或 OS——它们都可以正常工作。

但是,如果在 嵌入应用程序 运行 的网络浏览器中加载相同的网页 Android(在本例中为 TWebBrowser 组件),位置服务不工作。调用 navigator.geolocation.getCurrentPosition() 会导致 permission_denied 错误消息。甚至不会询问用户是否允许访问位置;它只会引发 permission_denied 错误。

我检查的一些显而易见的事情:

我在几台 Android 设备上尝试了它,这些设备具有不同的 Android 版本,一直到最新的 v6,它们的行为都是一样的。这让我觉得我 运行 喜欢 Android OS "feature"。有没有办法解决它,因为我的应用程序功能也取决于从网页访问定位服务。

作为一种解决方法,我在从应用程序调用时将当前位置传递给参数中的网页。但是当用户四处移动时,我无法使用 WatchLocation 来更新地图上的位置。

您可以先使用此代码检查权限

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }