Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting Blazor.Geolocation working with GA Blazor #14

Open
bdnts opened this issue Nov 27, 2019 · 4 comments
Open

Getting Blazor.Geolocation working with GA Blazor #14

bdnts opened this issue Nov 27, 2019 · 4 comments

Comments

@bdnts
Copy link

bdnts commented Nov 27, 2019

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 <script src="Location.js"></script> 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
Copy link

djaus2 commented Jun 27, 2020

#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
Copy link

djaus2 commented Jun 27, 2020

#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
Copy link

djaus2 commented Jun 27, 2020

image

djaus2 added a commit to djaus2/Blazor.Geolocation that referenced this issue Jun 27, 2020
Addresses issues in Issue AspNetMonsters#14
@djaus2
Copy link

djaus2 commented Jun 28, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants