AspNetMonsters / Blazor.Geolocation

Blazor interop for browers Geolocation apis
MIT License
91 stars 24 forks source link

Getting Blazor.Geolocation working with GA Blazor #14

Open bdnts opened 4 years ago

bdnts commented 4 years ago

Howdy Gents, I really, really wanted to use B.G, but ran into a slew of problems when trying to get it to work with VS16.9.3, and Core 3.0, Server Side Blazor. You guys are doing great work, there is no way I was going to get into your stuff and change it, so I've created an app with workarounds on all of the issues, and it successfully uses B.G to find location. My app can be found at https://github.com/bdnts/BlazorGeo and is accessible to all.

Issue #1: Location Services as a Singleton -- In the release I am using, IJSRuntime is already registered as Scoped. Workaround #1: Registered LocationService as Scoped. Running multiple browsers against the same app, each connection having its own service is probably not a bad idea.

Issue #2: OnInitAsync is deprecated.
Workaround #2: Change to OnInitializedAsync.

Issue #3: NullReferenceException. No matter what I did, just could not get this work during initialization.
Workaround #3: I change the logic to get location on a button click instead of during initialization. I commented out the old code and created GetMyLocation() to replace it.

Issue #4: Can't find Location.js. There is a missing step about adding to _Host.cshtml, but still could not find the script. Workaround #4: I created a local Location.js file, and copied the contents from B.G source tree into the local file.

Bada boom Bada bang, it works! Works really well. I deployed the app to Azure, drove around town on some errands, refreshed wherever I went. It's great! I'll be looking for updates, and will do what I can to help out.
Cheers Brian

djaus2 commented 4 years ago

1 Yes that solved teh service issue for me.

2 Yes but .. see #3

3 I handled that this way using OnAfterRenderAsync() as follows:

 Location location;

    protected async Task GetLocation()
    {
        location = await LocationService.GetLocationAsync();
    }

    protected  override async Task  OnAfterRenderAsync(bool first)
    {
        base.OnAfterRender(first);
        await GetLocation();
    }

Working other issues now.

djaus2 commented 4 years ago

4 Got it all working now by copying location.js to wwwroot (Note that is where to put it)

Some changes to code as above to stop recursion:

    Location location;

    protected async Task GetLocation()
    {
          location = await LocationService.GetLocationAsync();
          this.StateHasChanged();
    }

    protected  override async Task  OnAfterRenderAsync(bool first)
    {
        if (first)
        {
            base.OnAfterRender(first);
            await GetLocation();
        }
    }

    protected override void OnInitialized()
    {
    //Note there is now nothing wrt location here now.
    }

🥇 Thanks Brian Cheers David

djaus2 commented 4 years ago

image

djaus2 commented 4 years ago

Nb: I've pushed these changes to the ReadMe to the repository, as above.