简体   繁体   中英

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

I am using navigator.geolocation.getCurrentPosition() to get user's current location. The location feature on these web pages works fine when loaded directly into a web browser on both iOS and Android. User gets asked if they allow access to location, and it goes from there. Doesn't matter which web browser or OS--they all work fine.

However, if the same webpage is loaded in a web browser embedded in an app running on Android (TWebBrowser component in this case), the location service does not work. A call to navigator.geolocation.getCurrentPosition() results in permission_denied error message. User is NOT even asked if they would allow access to location; it just raises permission_denied error.

Some of the obvious things that I checked:

  • The code is preceded by if (navigator.geolocation) { so I know that geolocation capability is there.
  • The app is built and deployed with permissions to location services, which works fine as app accesses location services at other places in the code (outside of browser, that is).
  • The permission denied only happens in a webpage when its loaded in the web browser embedded in the app.
  • It only happens on Android. iOS works fine.
  • The web browser embedded in the app invokes the native web browser available on the device. That is, the web browser being used is not some special customized one. The app is built using Delphi Firemonkey, and uses TWebBrowser component.

I tried it on several Android devices with different Android versions all the way up to the latest v6, and they all behave the same. That leads me to think that I am running into some Android OS "feature". Is there any way around it as my apps functionality depends on access to location services from the webpage as well.

As a workaround, I am passing current location to the webpage in parameters when calling it from the app. But I can't use WatchLocation to update the position on map when user moves around.

you can use this code to check for permissions first

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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM