Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# BlazorDeviceInterop
Blazor library for interacting with browser Web APIs.

Currently offering:

- A GeolocationService to retrieve the latitude and longitude of your device from the browser.

## Setup
- Install NuGet package *Darnton.Blazor.DeviceInterop* into your Blazor project
- Add the AddScoped command to your services in the DI container (in Program.cs of your Blazor web project)
```
builder.Services
.AddScoped<IGeolocationService, GeolocationService>()
```
- In the code section of your Razor page, add a property to GeolocationService, with an inject attribute (so the DI can populate the property at run time)
```
[Inject]
public IGeolocationService GeolocationService { get; set; }
```

## Usage

Call *await GeolocationService.GetCurrentPosition()* to get your device's current location.

Example code:

```
var currentPositionResult = await GeolocationService.GetCurrentPosition();
var lat = currentPositionResult.Position.Coords.Latitude;
var lng = currentPositionResult.Position.Coords.Longitude;
var alt = currentPositionResult.Position.Coords.Altitude;
Console.WriteLine($"Current device location is latitude {lat}, longitude {lng}, altitude {alt}.");
```

## See Also

- Example code in BlazorDeviceTestRig folder of this github project
- [Geolocation in Blazor](https://darnton.co.nz/2020/11/29/geolocation-in-blazor/)