Blazor Hybrid WebView not able to Obtain GPS Location

See original GitHub issue

Description

I have created RCL Project which uses GoogleMap. This is purely done in Javascript/HTM/css using google maps Javascript SDK. I am trying to use this RCL in Blazor Hybrid Project. It does load the Map, but Location is denied.

image

Not sure if this was needed but i also entered the permission for android platform in AndroidManifest.xml, that did not help either.

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
	<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

	<uses-feature android:name="android.hardware.location" android:required="false" />
	<uses-feature android:name="android.hardware.location.gps" android:required="false" />
	<uses-feature android:name="android.hardware.location.network" android:required="false" />

Is there anything else that i need to do?

My location obtaining scripts are in JavaScript file (googleMaps.js) which is my custom JavaScript code that obtains location and plots the marker for current location. Additionally, it will also plot multiple marker for nearby business location.

In addition, I Manually gave location permission in the Android emulator, even that did not help

Steps to Reproduce

The steps to create are long.

Version with bug

Preview 13 (current)

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

android

Did you find any workaround?

No

Relevant log output

In the picture

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:23 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
jfversluiscommented, Feb 21, 2022

With the link you added earlier, I added this code to your MauiProgram.cs

        //This is need for enabling webView GPD & Camera Permission.
        //If you are not using WebView or using webview but don't need acces to GPS & Camera, this may not be needed at all
        builder.ConfigureMauiHandlers(handlers =>
        {
            handlers.AddHandler<IBlazorWebView, MauiBlazorWebViewHandler>();

            // Gerald: This was added
	   BlazorWebViewHandler.BlazorWebViewMapper.AppendToMapping("EnableGeoLocation", (handler, webview) =>
	   {
		handler.NativeView.Settings.SetGeolocationEnabled(true);
		handler.NativeView.Settings.JavaScriptEnabled = true;
		handler.NativeView.Settings.AllowFileAccess = true;
		handler.NativeView.Settings.SetGeolocationEnabled(true);
		handler.NativeView.Settings.SetGeolocationDatabasePath(handler.NativeView.Context.FilesDir.Path);
	   });
        });

Not 100% sure if these are all needed, but at least this makes it work 😃

image

Additionally I noticed a Google Maps API key in there… You might want to invalidate that one now 😉

1reaction
nssidhucommented, Feb 19, 2022

So i updated the repo to check if native GPS/Location would work and it does. so now in the repo i have native code that is working, but at the same time JavaScript/webview which is not working.

I would have ended up using native code and forget about obtaining location from JavaScript for Location, but native code does not have live tracking, i would have to get it via timer. I also noticed that getting location via native code is extremely slow as well.

As suggested by @Eilon there might be permission that need to be enabled for WebView/BlazorWebView since that is browser-based permission.

Now when I use the same code in Blazor wasm project, i don’t need to specify the permission upfront, when JavaScript Code (that will get gps coordinate) is about to execute it prompts user for permission. So, wondering if it is allowed to provide those permission upfront in the code, like we do in native code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blazor Hybrid MAUI APP : How to Access GPS location ...
I have been able to successfully load the Google MAP RCL Project into Blazor Hybrid, but not able to get the current location...
Read more >
ASP.NET Core Blazor Hybrid routing and navigation
Learn how to manage request routing and navigation in Blazor Hybrid apps.
Read more >
Blazor for .NET MAUI: What, How and When
In this example we're attempting to render a Main component (located within the MAUI Client App). You can have more than one BlazorWebView...
Read more >
".NET MAUI App" vs ".NET MAUI Blazor App"? : r/dotnet
When you run Blazor app inside a WebView, will the Blazor App have access to mobile features like notifications, GPS etc?
Read more >
MAUI vs Blazor: How it Works for Developers (Tutorial)
In this article, we'll take a closer look at Blazor and MAUI, and help you decide which one is right for your next...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found