Adding a JavaScript Function and Using the Geolocation API
Explore how to integrate JavaScript with Blazor WebAssembly by adding a custom JavaScript function that uses the Geolocation API. Learn how to retrieve the user's latitude and longitude, invoke JS functions from C#, and handle permission requests within the app.
We'll cover the following...
We'll cover the following...
Adding a JavaScript function
We now need to add a class to contain our current latitude and longitude. We will do this by following these steps:
- Right-click the
wwwrootfolder and select the “Add, New Folder” option from the menu. - Name the new folder
scripts. - Right-click the
scriptsfolder and select the “Add, New Item” option from the menu. - Enter “javascript” in the Search box.
- Select “JavaScript File.”
- Name the file
bweInterop.js. - Click the “Add” button.
Tip: In this course, we will be using the
bweInteropnamespace for our JavaScript code to both structure our code and minimize the risk of naming conflicts. - Enter the following JavaScript:
The preceding JavaScript code ...