mirror of
https://github.com/farcasclaudiu/BlazorDeviceInterop.git
synced 2026-06-22 07:01:03 +03:00
Add project files.
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
@page "/geolocation"
|
||||
@inherits GeolocationBase
|
||||
|
||||
<h1>Geolocation</h1>
|
||||
|
||||
<div class="my-3">
|
||||
<h3>Current Position</h3>
|
||||
<p class="my-0"><span class="font-weight-bold">Position </span>
|
||||
@if(CurrentPositionResult?.Position is null)
|
||||
{
|
||||
<span>[unknown]</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span>Lat: @CurrentLatitude °, Long: @CurrentLongitude °</span>
|
||||
}
|
||||
</p>
|
||||
<button class="btn btn-primary my-3" @onclick="ShowCurrentPosition">Current Location</button>
|
||||
@if (CurrentPositionResult?.Error != null)
|
||||
{
|
||||
<p class="bg-light text-danger">Error: @CurrentPositionResult.Error.Message</p>
|
||||
}
|
||||
<LeafletMap Map="PositionMap" TileLayer="OpenStreetMapsTileLayer" />
|
||||
</div>
|
||||
|
||||
<div class="my-3">
|
||||
<h3>Watch Position</h3>
|
||||
<p class="my-0">
|
||||
<span class="font-weight-bold">Last Position </span>
|
||||
@if (LastWatchPositionResult?.Position is null)
|
||||
{
|
||||
<span>[unknown]</span>
|
||||
}
|
||||
else
|
||||
{
|
||||
<span>Lat: @LastWatchLatitude °, Long: @LastWatchLongitude ° at @LastWatchTimestamp</span>
|
||||
}
|
||||
</p>
|
||||
<button class="btn btn-primary my-3" @onclick="TogglePositionWatch">@ToggleWatchCommand</button>
|
||||
Watch handler id: @WatchHandlerId
|
||||
<LeafletMap Map="WatchMap" TileLayer="OpenStreetMapsTileLayer" />
|
||||
</div>
|
||||
@@ -0,0 +1,124 @@
|
||||
using BlazorDeviceInterop.Components.LeafletMap;
|
||||
using BlazorDeviceInterop.Geolocation;
|
||||
using BlazorDeviceTestRig.Geolocation;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.JSInterop;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace BlazorDeviceTestRig.Pages
|
||||
{
|
||||
public class GeolocationBase : ComponentBase
|
||||
{
|
||||
[Inject] public IJSRuntime JSRuntime { get; set; }
|
||||
[Inject] public IGeolocationService GeolocationService { get; set; }
|
||||
|
||||
protected Map PositionMap;
|
||||
protected TileLayer OpenStreetMapsTileLayer;
|
||||
protected Marker CurrentPositionMarker;
|
||||
|
||||
protected Map WatchMap;
|
||||
protected Polyline WatchPath;
|
||||
protected List<Marker> WatchMarkers;
|
||||
|
||||
protected GeolocationResult CurrentPositionResult { get; set; }
|
||||
protected string CurrentLatitude => CurrentPositionResult?.Position?.Coords?.Latitude.ToString("F2");
|
||||
protected string CurrentLongitude => CurrentPositionResult?.Position?.Coords?.Longitude.ToString("F2");
|
||||
protected bool ShowCurrentPositionError => CurrentPositionResult?.Error != null;
|
||||
|
||||
private bool isWatching => WatchHandlerId.HasValue;
|
||||
protected long? WatchHandlerId { get; set; }
|
||||
protected GeolocationResult LastWatchPositionResult { get; set; }
|
||||
protected string LastWatchLatitude => LastWatchPositionResult?.Position?.Coords?.Latitude.ToString("F2");
|
||||
protected string LastWatchLongitude => LastWatchPositionResult?.Position?.Coords?.Longitude.ToString("F2");
|
||||
protected string LastWatchTimestamp => LastWatchPositionResult?.Position?.DateTime.ToString();
|
||||
protected string ToggleWatchCommand => isWatching ? "Stop watching" : "Start watching";
|
||||
|
||||
public GeolocationBase() : base()
|
||||
{
|
||||
PositionMap = new Map("geolocationPointMap", new MapOptions //Centred on New Zealand
|
||||
{
|
||||
Center = new LatLng(-42, 175),
|
||||
Zoom = 4
|
||||
});
|
||||
WatchMap = new Map("geolocationWatchMap", new MapOptions //Centred on New Zealand
|
||||
{
|
||||
Center = new LatLng(-42, 175),
|
||||
Zoom = 4
|
||||
});
|
||||
OpenStreetMapsTileLayer = new TileLayer(
|
||||
"https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
|
||||
new TileLayerOptions
|
||||
{
|
||||
Attribution = @"Map data © <a href=""https://www.openstreetmap.org/"">OpenStreetMap</a> contributors, <a href=""https://creativecommons.org/licenses/by-sa/2.0/"">CC-BY-SA</a>"
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public async void ShowCurrentPosition()
|
||||
{
|
||||
if (CurrentPositionMarker != null)
|
||||
{
|
||||
await CurrentPositionMarker.Remove();
|
||||
}
|
||||
CurrentPositionResult = await GeolocationService.GetCurrentPosition();
|
||||
if (CurrentPositionResult.IsSuccess)
|
||||
{
|
||||
CurrentPositionMarker = new Marker(
|
||||
CurrentPositionResult.Position.ToLeafletLatLng(), null
|
||||
);
|
||||
await CurrentPositionMarker.BindToJsRuntime(JSRuntime);
|
||||
await CurrentPositionMarker.AddTo(PositionMap);
|
||||
}
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
public async void TogglePositionWatch()
|
||||
{
|
||||
if (isWatching)
|
||||
{
|
||||
GeolocationService.WatchPositionReceived -= HandleWatchPositionReceived;
|
||||
await GeolocationService.ClearWatch(WatchHandlerId.Value);
|
||||
WatchHandlerId = null;
|
||||
foreach (var marker in WatchMarkers)
|
||||
{
|
||||
await marker.Remove();
|
||||
}
|
||||
WatchMarkers.Clear();
|
||||
await WatchPath.Remove();
|
||||
WatchPath = null;
|
||||
}
|
||||
else
|
||||
{
|
||||
GeolocationService.WatchPositionReceived += HandleWatchPositionReceived;
|
||||
WatchHandlerId = await GeolocationService.WatchPosition();
|
||||
}
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
private async void HandleWatchPositionReceived(object sender, GeolocationEventArgs e)
|
||||
{
|
||||
LastWatchPositionResult = e.GeolocationResult;
|
||||
if (LastWatchPositionResult.IsSuccess)
|
||||
{
|
||||
var latlng = LastWatchPositionResult.Position.ToLeafletLatLng();
|
||||
var marker = new Marker(latlng, null);
|
||||
if (WatchPath is null)
|
||||
{
|
||||
WatchMarkers = new List<Marker> { marker };
|
||||
WatchPath = new Polyline(WatchMarkers.Select(m => m.LatLng), new PolylineOptions());
|
||||
await WatchPath.BindToJsRuntime(JSRuntime);
|
||||
await WatchPath.AddTo(WatchMap);
|
||||
}
|
||||
else
|
||||
{
|
||||
WatchMarkers.Add(marker);
|
||||
await WatchPath.AddLatLng(latlng);
|
||||
}
|
||||
await marker.BindToJsRuntime(JSRuntime);
|
||||
await marker.AddTo(WatchMap);
|
||||
}
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
@page "/"
|
||||
|
||||
<h1>Hello, world!</h1>
|
||||
|
||||
Welcome to your new app.
|
||||
Reference in New Issue
Block a user