Blazor Bootstrap 組件庫地理定位/移動距離追蹤組件介紹
- 2022 年 4 月 15 日
- 筆記
- Blazor, Blazor組件, BootstrapBlazor, 瀏覽器API
地理定位/移動距離追蹤組件
通過瀏覽器 API 獲取定位信息
DEMO //www.blazor.zone/geolocations
小提示
注意: 出於安全考慮,當網頁請求獲取用戶位置信息時,用戶會被提示進行授權。注意不同瀏覽器在請求權限時有不同的策略和方式。Windows10 在未開啟定位的情況下無法獲取位置。
示例
dotnet new blazorserver -o b07geo
dotnet add b07geo package BootstrapBlazor
dotnet add b07geo package BootstrapBlazor.FontAwesome
dotnet sln add b07geo/b07geo.csproj
篇幅有限,餘下步驟參考: //www.cnblogs.com/densen2014/p/16147322.html
例 Index.razor
<h3>地理定位/移動距離追蹤</h3>
<div>
<p>單擊按鈕以獲取地理位置坐標。</p>
@if (WatchID == 0)
{
<Button Text="獲取位置" OnClick="GetLocation"></Button>
<Button Text="移動距離追蹤" OnClick="WatchPosition"></Button>
}
else
{
<Button Text="停止追蹤" OnClick="ClearWatchPosition"></Button>
}
@if (Model != null)
{
<div class="form-inline row g-3 mt-3">
<div class="col-12 col-sm-4">
<Display Value="@Model.Longitude" ShowLabel="true" DisplayText="經度" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Latitude" ShowLabel="true" DisplayText="緯度" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Accuracy" ShowLabel="true" DisplayText="位置精度" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Altitude" ShowLabel="true" DisplayText="海拔" />
</div>
<div class="col-12 col-sm-4">
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.AltitudeAccuracy" ShowLabel="true" DisplayText="海拔精度" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Heading" ShowLabel="true" DisplayText="方向" />
</div>
<div class="col-12 col-sm-4">
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Speed" ShowLabel="true" DisplayText="速度" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.CurrentDistance" ShowLabel="true" DisplayText="移動距離" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.TotalDistance" ShowLabel="true" DisplayText="總移動距離" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.LastUpdateTime" ShowLabel="true" DisplayText="時間戳" />
</div>
</div>
}
@Trace
</div>
cs文件, 例 Index.razor.cs
using BootstrapBlazor.Components;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using Microsoft.JSInterop;
namespace b07geo.Pages;
public partial class Index : IAsyncDisposable
{
private JSInterop<Index>? Interop { get; set; }
private string Trace;
[Inject]
private IJSRuntime? JSRuntime { get; set; }
private GeolocationItem? Model { get; set; }
/// <summary>
/// 獲得/設置 獲取持續定位監聽器ID
/// </summary>
private long WatchID { get; set; }
private async Task GetLocation()
{
Interop ??= new JSInterop<Index>(JSRuntime);
var ret = await Geolocation.GetLocaltion(Interop, this, nameof(GetLocationCallback));
Trace += (ret ? "成功獲取定位" : "獲取定位失敗");
}
private async Task WatchPosition()
{
try
{
Interop ??= new JSInterop<Index>(JSRuntime);
WatchID = await Geolocation.WatchPosition(Interop, this, nameof(GetLocationCallback));
Trace += WatchID != 0 ? "調用 WatchPosition 成功" : "調用 WatchPosition 失敗";
Trace += $"WatchID : {WatchID}";
}
catch (Exception)
{
Trace += "調用 WatchPosition 失敗";
}
}
private async Task ClearWatchPosition()
{
if (WatchID != 0)
{
Interop ??= new JSInterop<Index>(JSRuntime);
var ret = await Geolocation.ClearWatchPosition(Interop, WatchID);
if (ret)
{
WatchID = 0;
}
Trace += ret ? "停止追蹤成功" : "停止追蹤失敗";
}
}
/// <summary>
///
/// </summary>
/// <param name="item"></param>
[JSInvokable]
public void GetLocationCallback(GeolocationItem item)
{
Model = item;
StateHasChanged();
}
/// <summary>
///
/// </summary>
/// <param name="disposing"></param>
protected virtual async ValueTask DisposeAsync(bool disposing)
{
if (disposing)
{
if (Interop != null)
{
if (WatchID != 0)
{
await Geolocation.ClearWatchPosition(Interop, WatchID);
}
Interop.Dispose();
Interop = null;
}
}
}
/// <summary>
///
/// </summary>
public async ValueTask DisposeAsync()
{
await DisposeAsync(true);
GC.SuppressFinalize(this);
}
}
模擬追蹤定位
chrome/edge F12進入調試模式後,點擊右上角的 三個點的標誌, 選擇更多工具, 傳感器, 定位
選擇一個地理位置,組件定位追蹤開啟後,可以慢慢調節參數測試組件功能. :->
Blazor Bootstrap 組件庫文檔
寫在最後
希望大佬們看到這篇文章,能給項目點個star支持下,感謝各位!
star流程:
1、訪問點擊項目鏈接:BootstrapBlazor
//gitee.com/LongbowEnterprise/BootstrapBlazor
2、點擊star,如下圖,即可完成star,關注項目不迷路:
另外還有兩個GVP項目,大佬們方便的話也點下star唄,非常感謝:
BootstrapAdmin 項目地址:
//gitee.com/LongbowEnterprise/BootstrapAdmin
SliderCaptcha 項目地址:
//gitee.com/LongbowEnterprise/SliderCaptcha
交流群(QQ)歡迎加群討論
BA & Blazor ①(795206915) BA & Blazor ②(675147445)