Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyName>AspNetMonsters.Blazor.Geolocation</AssemblyName>
<RootNamespace>AspNetMonsters.Blazor.Geolocation</RootNamespace>
<Version>0.5.0-preview1</Version>
<Version>0.5.0-preview2</Version>

<DefaultItemExcludes>${DefaultItemExcludes};node_modules\**</DefaultItemExcludes>
</PropertyGroup>
Expand All @@ -23,16 +23,20 @@
<Content Remove="package.json" />
<Content Remove="tsconfig.json" />
</ItemGroup>

<ItemGroup>
<None Include="package-lock.json" />
<None Include="package.json" />
<None Include="tsconfig.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.0.0-rc1.19457.4" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.0.0-rc1.19457.4" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.0.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="wwwroot"></Folder>
</ItemGroup>

</Project>
</Project>
2 changes: 1 addition & 1 deletion AspNetMonsters.Blazor.Geolocation/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dom"
],
"strict": true,
"outDir": "./content/"
"outDir": "./wwwroot/"
},
"exclude": [
"node_modules"
Expand Down
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ This package provides Blazor applications with access to the browser's [Geolocat

1) In your Blazor app's `Startup.cs`, register the 'LocationService'.

###### Blazor Server
```csharp
public void ConfigureServices(IServiceCollection services)
{
...
services.AddScoped<LocationService>();
...
}
```
###### Blazor WebAssembly
```csharp
public void ConfigureServices(IServiceCollection services)
{
...
Expand All @@ -21,9 +31,43 @@ This package provides Blazor applications with access to the browser's [Geolocat
}
```

1) Add the link to the Location.js script in:

- _Host.cshtml for Blazor Server
- index.html for Blazor WebAssembly

```html
<script src="_content/AspNetMonsters.Blazor.Geolocation/Location.js"></script>
```
1) Now you can inject the LocationService into any Blazor page and use it like this:

###### Blazor Server
Call the LocationService in the OnAfterRenderAsync method
```
@using AspNetMonsters.Blazor.Geolocation
@inject LocationService LocationService
<h3>You are here</h3>
<div>
Lat: @location?.Latitude <br/>
Long: @location?.Longitude <br />
Accuracy: @location?.Accuracy <br />
</div>

@functions
{
Location location;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
location = await LocationService.GetLocationAsync();
StateHasChanged();
}
}
```

###### Blazor WebAssembly
Call the LocationService in the OnInitializedAsync method
```
@using AspNetMonsters.Blazor.Geolocation
@inject LocationService LocationService
<h3>You are here</h3>
Expand All @@ -37,7 +81,7 @@ This package provides Blazor applications with access to the browser's [Geolocat
{
Location location;

protected override async Task OnInitAsync()
protected override async Task OnInitializedAsync()
{
location = await LocationService.GetLocationAsync();
}
Expand Down