mirror of
https://github.com/farcasclaudiu/BlazorDeviceInterop.git
synced 2026-06-22 07:01:03 +03:00
Reorganise project structure and namespaces. Remove Leaflet project and replace with Darnton.Blazor.Leaflet package.
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Razor">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<RazorLangVersion>3.0</RazorLangVersion>
|
||||
<RootNamespace>Darnton.Blazor.DeviceInterop</RootNamespace>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
<Version>0.1.0</Version>
|
||||
<Authors>Bernard Darnton</Authors>
|
||||
<Product>Blazor device interop library</Product>
|
||||
<Description>Blazor device interop library including wrappers for Geolocation API</Description>
|
||||
<Copyright>2020 Bernard Darnton</Copyright>
|
||||
<PackageLicenseExpression>MIT</PackageLicenseExpression>
|
||||
<RepositoryUrl>https://github.com/darnton/BlazorDeviceInterop</RepositoryUrl>
|
||||
<AssemblyVersion>0.0.1.0</AssemblyVersion>
|
||||
<FileVersion>0.0.1.0</FileVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<DocumentationFile>C:\Users\user\source\repos\BlazorDeviceInterop\Darnton.Blazor.DeviceInterop\Darnton.Blazor.DeviceInterop.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Darnton.Units" Version="0.1.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.8" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.8" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,231 @@
|
||||
<?xml version="1.0"?>
|
||||
<doc>
|
||||
<assembly>
|
||||
<name>Darnton.Blazor.DeviceInterop</name>
|
||||
</assembly>
|
||||
<members>
|
||||
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates">
|
||||
<summary>
|
||||
Geolocation Coordinates, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates"/>.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.Latitude">
|
||||
<summary>
|
||||
Latitude in decimal degrees.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.Longitude">
|
||||
<summary>
|
||||
Longitude in decimal degrees.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.Altitude">
|
||||
<summary>
|
||||
Altitude in metres, relative to sea level.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.Accuracy">
|
||||
<summary>
|
||||
Accuracy of the latitude and longitude properties, in metres.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.AltitudeAccuracy">
|
||||
<summary>
|
||||
Accuracy of the altitude, in metres.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.Heading">
|
||||
<summary>
|
||||
The direction the device is travelling, in degrees clockwise from true north.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationCoordinates.Speed">
|
||||
<summary>
|
||||
The velocity of the device, in metres per second.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationEventArgs">
|
||||
<summary>
|
||||
Geolocation event data. Provides the <see cref="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationEventArgs.GeolocationResult"/> with the position or error associated with the event.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationEventArgs.GeolocationResult">
|
||||
<summary>
|
||||
The <see cref="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationEventArgs.GeolocationResult"/> associated with the event.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition">
|
||||
<summary>
|
||||
Geolocation Position, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPosition"/>.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition.Coords">
|
||||
<summary>
|
||||
The coordinates defining the current location
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition.Timestamp">
|
||||
<summary>
|
||||
The time the coordinates were taken, in milliseconds since the Unix epoch.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition.DateTimeOffset">
|
||||
<summary>
|
||||
The <see cref="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition.DateTimeOffset"/> derived from the Timestamp, in UTC.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionError">
|
||||
<summary>
|
||||
The reason for a Geolocation error, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError"/>.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionError.Code">
|
||||
<summary>
|
||||
The <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionErrorCode"/> for the error.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionError.Message">
|
||||
<summary>
|
||||
Details of the error. Intended for debugging rather than display to the user.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionErrorCode">
|
||||
<summary>
|
||||
An enumeration of error codes used by <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionError"/>.
|
||||
<see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError/code"/>.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionErrorCode.DEVICE_NOT_SUPPORTED">
|
||||
<summary>
|
||||
Geolocation failoed because the device does not support geolocation. Not part of W3C spec.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionErrorCode.PERMISSION_DENIED">
|
||||
<summary>
|
||||
Geolocation failed because permission to access location was denied.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionErrorCode.POSITION_UNAVAILABLE">
|
||||
<summary>
|
||||
Geolocation failed because of an internal error on the device.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="F:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionErrorCode.TIMEOUT">
|
||||
<summary>
|
||||
Geolocation failed because no position was returned in time.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationResult">
|
||||
<summary>
|
||||
The result of a geolocation request. Contains either a <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition"/> or a <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionError"/>.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationResult.Position">
|
||||
<summary>
|
||||
The <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPosition"/> returned on successful geolocation.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationResult.Error">
|
||||
<summary>
|
||||
The <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationPositionError"/> returned by a failed geolocation attempt.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationResult.IsSuccess">
|
||||
<summary>
|
||||
Indicates whether the geolocation attempt was successful.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService">
|
||||
<summary>
|
||||
An implementation of <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService"/> that provides
|
||||
an interop layer for the device's Geolocation API.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="E:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.WatchPositionReceived">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.#ctor(Microsoft.JSInterop.IJSRuntime)">
|
||||
<summary>
|
||||
Constructs a <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService"/> object.
|
||||
</summary>
|
||||
<param name="JSRuntime"></param>
|
||||
</member>
|
||||
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.GetCurrentPosition(Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions)">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.WatchPosition(Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions)">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.SetWatchPosition(Darnton.Blazor.DeviceInterop.Geolocation.GeolocationResult)">
|
||||
<summary>
|
||||
Invokes the <see cref="E:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.WatchPositionReceived"/> event handler.
|
||||
Invoked by the success and error callbacks of the JavaScript watchPosition() function.
|
||||
</summary>
|
||||
<param name="watchResult">A <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationResult"/> passed back from JavaScript.</param>
|
||||
</member>
|
||||
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationService.ClearWatch(System.Int64)">
|
||||
<inheritdoc/>
|
||||
</member>
|
||||
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService">
|
||||
<summary>
|
||||
A wrapper around the device's Geolocation API services.
|
||||
<see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API"/>.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.GetCurrentPosition(Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions)">
|
||||
<summary>
|
||||
A wrapper around the <see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition"/> function,
|
||||
used to get the current position of the device.
|
||||
</summary>
|
||||
<param name="options"><see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions"/> used to modify the request.</param>
|
||||
<returns>The result of the geolocation request.</returns>
|
||||
</member>
|
||||
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.WatchPosition(Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions)">
|
||||
<summary>
|
||||
A wrapper around the <see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition"/> function,
|
||||
used to listen for position changes. If the service is listening, a <see cref="E:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.WatchPositionReceived"/> event is fired
|
||||
each time the device's position changes.
|
||||
</summary>
|
||||
<param name="options"><see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions"/> used to modify the request.</param>
|
||||
<returns>A watch ID that refers to the handler. The ID can be used to unregister the handler with <see cref="M:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.ClearWatch(System.Int64)"/>.</returns>
|
||||
</member>
|
||||
<member name="E:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.WatchPositionReceived">
|
||||
<summary>
|
||||
Handles the receipt of new positions. Fired whenever a handler is registered and the device's position changes.
|
||||
Invoked with the sender and the <see cref="T:Darnton.Blazor.DeviceInterop.Geolocation.GeolocationEventArgs"/>.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.ClearWatch(System.Int64)">
|
||||
<summary>
|
||||
A wrapper around the <see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/clearWatch"/> function,
|
||||
used to unregister a handler created with <see cref="M:Darnton.Blazor.DeviceInterop.Geolocation.IGeolocationService.WatchPosition(Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions)"/>.
|
||||
</summary>
|
||||
<param name="watchId">The ID of the registered watch handler.</param>
|
||||
<returns>A task that represents the async clear operation.</returns>
|
||||
</member>
|
||||
<member name="T:Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions">
|
||||
<summary>
|
||||
Option properties to be passed to Geolocation functions, based on https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions.EnableHighAccuracy">
|
||||
<summary>
|
||||
Enable high accuracy mode for best possible results.
|
||||
May be slower or increase power consumption. Defaults to false.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions.Timeout">
|
||||
<summary>
|
||||
Maximum length of time allowed to return a position (in milliseconds).
|
||||
Set to null for no timeout. Defaults to null.
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:Darnton.Blazor.DeviceInterop.Geolocation.PositionOptions.MaximumAge">
|
||||
<summary>
|
||||
Maximum allowed age for a cached result.
|
||||
Set to null to disregard the age of cached results.
|
||||
Set to 0 to skip the cache and attempt a fresh result every time. Defaults to 0.
|
||||
</summary>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
|
||||
namespace Darnton.Blazor.DeviceInterop.Geolocation
|
||||
{
|
||||
/// <summary>
|
||||
/// Geolocation Coordinates, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates"/>.
|
||||
/// </summary>
|
||||
public class GeolocationCoordinates
|
||||
{
|
||||
/// <summary>
|
||||
/// Latitude in decimal degrees.
|
||||
/// </summary>
|
||||
public double Latitude { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Longitude in decimal degrees.
|
||||
/// </summary>
|
||||
public double Longitude { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Altitude in metres, relative to sea level.
|
||||
/// </summary>
|
||||
public double? Altitude { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Accuracy of the latitude and longitude properties, in metres.
|
||||
/// </summary>
|
||||
public double Accuracy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Accuracy of the altitude, in metres.
|
||||
/// </summary>
|
||||
public double? AltitudeAccuracy { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The direction the device is travelling, in degrees clockwise from true north.
|
||||
/// </summary>
|
||||
public double? Heading { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The velocity of the device, in metres per second.
|
||||
/// </summary>
|
||||
public double? Speed { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace Darnton.Blazor.DeviceInterop.Geolocation
|
||||
{
|
||||
/// <summary>
|
||||
/// Geolocation event data. Provides the <see cref="GeolocationResult"/> with the position or error associated with the event.
|
||||
/// </summary>
|
||||
public class GeolocationEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="GeolocationResult"/> associated with the event.
|
||||
/// </summary>
|
||||
public GeolocationResult GeolocationResult { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Darnton.Units;
|
||||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Darnton.Blazor.DeviceInterop.Geolocation
|
||||
{
|
||||
/// <summary>
|
||||
/// Geolocation Position, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPosition"/>.
|
||||
/// </summary>
|
||||
public class GeolocationPosition
|
||||
{
|
||||
/// <summary>
|
||||
/// The coordinates defining the current location
|
||||
/// </summary>
|
||||
public GeolocationCoordinates Coords { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The time the coordinates were taken, in milliseconds since the Unix epoch.
|
||||
/// </summary>
|
||||
public long Timestamp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="DateTimeOffset"/> derived from the Timestamp, in UTC.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public DateTimeOffset DateTimeOffset => (Timestamp / 1000).FromUnixTime().ToDateTimeOffset();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace Darnton.Blazor.DeviceInterop.Geolocation
|
||||
{
|
||||
/// <summary>
|
||||
/// The reason for a Geolocation error, based on <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError"/>.
|
||||
/// </summary>
|
||||
public class GeolocationPositionError
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="GeolocationPositionErrorCode"/> for the error.
|
||||
/// </summary>
|
||||
public GeolocationPositionErrorCode Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Details of the error. Intended for debugging rather than display to the user.
|
||||
/// </summary>
|
||||
public string Message { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
namespace Darnton.Blazor.DeviceInterop.Geolocation
|
||||
{
|
||||
/// <summary>
|
||||
/// An enumeration of error codes used by <see cref="GeolocationPositionError"/>.
|
||||
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/GeolocationPositionError/code"/>.
|
||||
/// </summary>
|
||||
public enum GeolocationPositionErrorCode
|
||||
{
|
||||
/// <summary>
|
||||
/// Geolocation failoed because the device does not support geolocation. Not part of W3C spec.
|
||||
/// </summary>
|
||||
DEVICE_NOT_SUPPORTED = 0,
|
||||
/// <summary>
|
||||
/// Geolocation failed because permission to access location was denied.
|
||||
/// </summary>
|
||||
PERMISSION_DENIED = 1,
|
||||
/// <summary>
|
||||
/// Geolocation failed because of an internal error on the device.
|
||||
/// </summary>
|
||||
POSITION_UNAVAILABLE = 2,
|
||||
/// <summary>
|
||||
/// Geolocation failed because no position was returned in time.
|
||||
/// </summary>
|
||||
TIMEOUT = 3
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Darnton.Blazor.DeviceInterop.Geolocation
|
||||
{
|
||||
/// <summary>
|
||||
/// The result of a geolocation request. Contains either a <see cref="GeolocationPosition"/> or a <see cref="GeolocationPositionError"/>.
|
||||
/// </summary>
|
||||
public class GeolocationResult
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="GeolocationPosition"/> returned on successful geolocation.
|
||||
/// </summary>
|
||||
public GeolocationPosition Position { get; set; }
|
||||
/// <summary>
|
||||
/// The <see cref="GeolocationPositionError"/> returned by a failed geolocation attempt.
|
||||
/// </summary>
|
||||
public GeolocationPositionError Error { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the geolocation attempt was successful.
|
||||
/// </summary>
|
||||
[JsonIgnore]
|
||||
public bool IsSuccess => !(Position is null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using Microsoft.JSInterop;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Darnton.Blazor.DeviceInterop.Geolocation
|
||||
{
|
||||
/// <summary>
|
||||
/// An implementation of <see cref="IGeolocationService"/> that provides
|
||||
/// an interop layer for the device's Geolocation API.
|
||||
/// </summary>
|
||||
public class GeolocationService : IGeolocationService
|
||||
{
|
||||
private readonly IJSRuntime _jsRuntime;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public event EventHandler<GeolocationEventArgs> WatchPositionReceived;
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a <see cref="GeolocationService"/> object.
|
||||
/// </summary>
|
||||
/// <param name="JSRuntime"></param>
|
||||
public GeolocationService(IJSRuntime JSRuntime)
|
||||
{
|
||||
_jsRuntime = JSRuntime;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<GeolocationResult> GetCurrentPosition(PositionOptions options = null)
|
||||
{
|
||||
return await _jsRuntime.InvokeAsync<GeolocationResult>("Geolocation.getCurrentPosition", options);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task<long?> WatchPosition(PositionOptions options = null)
|
||||
{
|
||||
var callbackObj = DotNetObjectReference.Create(this);
|
||||
return await _jsRuntime.InvokeAsync<int>("Geolocation.watchPosition",
|
||||
callbackObj, nameof(SetWatchPosition), options);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Invokes the <see cref="WatchPositionReceived"/> event handler.
|
||||
/// Invoked by the success and error callbacks of the JavaScript watchPosition() function.
|
||||
/// </summary>
|
||||
/// <param name="watchResult">A <see cref="GeolocationResult"/> passed back from JavaScript.</param>
|
||||
[JSInvokable]
|
||||
public void SetWatchPosition(GeolocationResult watchResult)
|
||||
{
|
||||
WatchPositionReceived?.Invoke(this, new GeolocationEventArgs
|
||||
{
|
||||
GeolocationResult = watchResult
|
||||
});
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task ClearWatch(long watchId)
|
||||
{
|
||||
await _jsRuntime.InvokeVoidAsync("Geolocation.clearWatch", watchId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Darnton.Blazor.DeviceInterop.Geolocation
|
||||
{
|
||||
/// <summary>
|
||||
/// A wrapper around the device's Geolocation API services.
|
||||
/// <see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API"/>.
|
||||
/// </summary>
|
||||
public interface IGeolocationService
|
||||
{
|
||||
/// <summary>
|
||||
/// A wrapper around the <see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition"/> function,
|
||||
/// used to get the current position of the device.
|
||||
/// </summary>
|
||||
/// <param name="options"><see cref="PositionOptions"/> used to modify the request.</param>
|
||||
/// <returns>The result of the geolocation request.</returns>
|
||||
Task<GeolocationResult> GetCurrentPosition(PositionOptions options = null);
|
||||
/// <summary>
|
||||
/// A wrapper around the <see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition"/> function,
|
||||
/// used to listen for position changes. If the service is listening, a <see cref="WatchPositionReceived"/> event is fired
|
||||
/// each time the device's position changes.
|
||||
/// </summary>
|
||||
/// <param name="options"><see cref="PositionOptions"/> used to modify the request.</param>
|
||||
/// <returns>A watch ID that refers to the handler. The ID can be used to unregister the handler with <see cref="ClearWatch(long)"/>.</returns>
|
||||
Task<long?> WatchPosition(PositionOptions options = null);
|
||||
/// <summary>
|
||||
/// Handles the receipt of new positions. Fired whenever a handler is registered and the device's position changes.
|
||||
/// Invoked with the sender and the <see cref="GeolocationEventArgs"/>.
|
||||
/// </summary>
|
||||
event EventHandler<GeolocationEventArgs> WatchPositionReceived;
|
||||
/// <summary>
|
||||
/// A wrapper around the <see href="https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/clearWatch"/> function,
|
||||
/// used to unregister a handler created with <see cref="WatchPosition(PositionOptions)"/>.
|
||||
/// </summary>
|
||||
/// <param name="watchId">The ID of the registered watch handler.</param>
|
||||
/// <returns>A task that represents the async clear operation.</returns>
|
||||
Task ClearWatch(long watchId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace Darnton.Blazor.DeviceInterop.Geolocation
|
||||
{
|
||||
/// <summary>
|
||||
/// Option properties to be passed to Geolocation functions, based on https://developer.mozilla.org/en-US/docs/Web/API/PositionOptions.
|
||||
/// </summary>
|
||||
public class PositionOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// Enable high accuracy mode for best possible results.
|
||||
/// May be slower or increase power consumption. Defaults to false.
|
||||
/// </summary>
|
||||
public bool EnableHighAccuracy { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum length of time allowed to return a position (in milliseconds).
|
||||
/// Set to null for no timeout. Defaults to null.
|
||||
/// </summary>
|
||||
public long? Timeout { get; set; } = null;
|
||||
|
||||
/// <summary>
|
||||
/// Maximum allowed age for a cached result.
|
||||
/// Set to null to disregard the age of cached results.
|
||||
/// Set to 0 to skip the cache and attempt a fresh result every time. Defaults to 0.
|
||||
/// </summary>
|
||||
public long? MaximumAge { get; set; } = 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
@using Microsoft.AspNetCore.Components.Web
|
||||
@@ -0,0 +1,65 @@
|
||||
window.Geolocation = {
|
||||
|
||||
getCurrentPosition: async function (options) {
|
||||
var result = { position: null, error: null };
|
||||
var getCurrentPositionPromise = new Promise((resolve, reject) => {
|
||||
if (!navigator.geolocation) {
|
||||
reject({ code: 0, message: 'This device does not support geolocation.' });
|
||||
} else {
|
||||
navigator.geolocation.getCurrentPosition(resolve, reject, options);
|
||||
}
|
||||
});
|
||||
await getCurrentPositionPromise.then(
|
||||
(position) => { this.mapPositionToResult(position, result) }
|
||||
).catch(
|
||||
(error) => { this.mapErrorToResult(error, result) }
|
||||
);
|
||||
return result;
|
||||
},
|
||||
|
||||
watchPosition: async function (dotNetCallbackRef, callbackMethod, options) {
|
||||
if (!navigator.geolocation) return null;
|
||||
|
||||
return navigator.geolocation.watchPosition(
|
||||
(position) => {
|
||||
let result = { position: null, error: null };
|
||||
this.mapPositionToResult(position, result);
|
||||
dotNetCallbackRef.invokeMethodAsync(callbackMethod, result);
|
||||
},
|
||||
(error) => {
|
||||
let result = { position: null, error: null };
|
||||
this.mapErrorToResult(error, result);
|
||||
dotNetCallbackRef.invokeMethodAsync(callbackMethod, result);
|
||||
},
|
||||
options);
|
||||
},
|
||||
|
||||
clearWatch: function (id) {
|
||||
if (navigator.geolocation) {
|
||||
navigator.geolocation.clearWatch(id);
|
||||
}
|
||||
},
|
||||
|
||||
mapPositionToResult: function (position, result) {
|
||||
result.position = {
|
||||
coords: {
|
||||
latitude: position.coords.latitude,
|
||||
longitude: position.coords.longitude,
|
||||
altitude: position.coords.altitude,
|
||||
accuracy: position.coords.accuracy,
|
||||
altitudeAccuracy: position.coords.altitudeAccuracy,
|
||||
heading: position.coords.heading,
|
||||
speed: position.coords.speed
|
||||
},
|
||||
timestamp: position.timestamp
|
||||
}
|
||||
},
|
||||
|
||||
mapErrorToResult: function (error, result) {
|
||||
result.error = {
|
||||
code: error.code,
|
||||
message: code.message
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user