Skip to content

Commit

Permalink
doc: update Geolocations demos (#875)
Browse files Browse the repository at this point in the history
Co-authored-by: Argo Zhang <argo@live.ca>
  • Loading branch information
h2ls and ArgoZhang authored Mar 29, 2023
1 parent 69b7c52 commit bfc9531
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 172 deletions.
146 changes: 146 additions & 0 deletions src/BootstrapBlazor.Shared/Demos/Geolocations/GeolocationNormal.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
@using BootstrapBlazor.Shared.Samples
@inject IStringLocalizer<Geolocations> Localizer
@inject IJSRuntime JSRuntime
@implements IAsyncDisposable

<div>
@if (WatchID == 0)
{
<Button Text="@Localizer["GetLocationButtonText"]" OnClick="GetLocation"></Button>
<Button Text="@Localizer["WatchPositionButtonText"]" OnClick="WatchPosition"></Button>
}
else
{
<Button Text="@Localizer["ClearWatchPositionButtonText"]" 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="@Localizer["Longitude"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Latitude" ShowLabel="true" DisplayText="@Localizer["Latitude"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Accuracy" ShowLabel="true" DisplayText="@Localizer["Accuracy"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Altitude" ShowLabel="true" DisplayText="@Localizer["Altitude"]" />
</div>
<div class="col-12 col-sm-4">
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.AltitudeAccuracy" ShowLabel="true" DisplayText="@Localizer["AltitudeAccuracy"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Heading" ShowLabel="true" DisplayText="@Localizer["Heading"]" />
</div>
<div class="col-12 col-sm-4">
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Speed" ShowLabel="true" DisplayText="@Localizer["Speed"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.CurrentDistance" ShowLabel="true" DisplayText="@Localizer["CurrentDistance"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.TotalDistance" ShowLabel="true" DisplayText="@Localizer["TotalDistance"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.LastUpdateTime" ShowLabel="true" DisplayText="@Localizer["LastUpdateTime"]" />
</div>
</div>
}
<BlockLogger @ref="Trace" class="mt-3" />
</div>

@code {
private JSInterop<GeolocationNormal>? Interop { get; set; }

[NotNull]
private BlockLogger? Trace { get; set; }

private GeolocationItem? Model { get; set; }

/// <summary>
/// 获得/设置 获取持续定位监听器ID
/// </summary>
private long WatchID { get; set; }

private async Task GetLocation()
{
Interop ??= new JSInterop<GeolocationNormal>(JSRuntime);
var ret = await Geolocation.GetLocaltion(interop: Interop, component: this, callbackMethodName: nameof(GetLocationCallback));
Trace.Log(ret ? Localizer["GetLocationResultSuccess"] : Localizer["GetLocationResultFailed"]);
}
private async Task WatchPosition()
{
try
{
Interop ??= new JSInterop<GeolocationNormal>(JSRuntime);
WatchID = await Geolocation.WatchPosition(Interop, this, nameof(GetLocationCallback));
Trace.Log(WatchID != 0 ? Localizer["WatchPositionResultSuccess"] : Localizer["WatchPositionResultFailed"]);
Trace.Log($"WatchID : {WatchID}");
}
catch (Exception)
{
Trace.Log(Localizer["WatchPositionResultFailed"]);
}
}

private async Task ClearWatchPosition()
{
if (WatchID != 0)
{
Interop ??= new JSInterop<GeolocationNormal>(JSRuntime);
var ret = await Geolocation.ClearWatchPosition(Interop, WatchID);
if (ret)
{
WatchID = 0;
}
Trace.Log(ret ? Localizer["ClearWatchPositionResultSuccess"] : Localizer["ClearWatchPositionResultFailed"]);
}
}

/// <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);
}
}
10 changes: 5 additions & 5 deletions src/BootstrapBlazor.Shared/Locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5944,11 +5944,11 @@
"ImageViewerErrorTemplateLoadFailed": "Load failed"
},
"BootstrapBlazor.Shared.Samples.Geolocations": {
"Title": "Geolocation",
"BaseUsageText": "Basic usage",
"IntroText1": "Obtain location information through browser API.",
"IntroText2": "Click the button to get your coordinates.",
"IntroText3": "Note: For security reasons, when a web page tries to access location information, the user is notified and asked to grant permission. Be aware that each browser has its own policies and methods for requesting this permission.",
"GeolocationsTitle": "Geolocation",
"GeolocationNormalText": "Basic usage",
"GeolocationNormalIntro": "Obtain location information through browser API.",
"GeolocationNormalIntro2": "Click the button to get your coordinates.",
"GeolocationNormalIntro3": "Note: For security reasons, when a web page tries to access location information, the user is notified and asked to grant permission. Be aware that each browser has its own policies and methods for requesting this permission.",
"GetLocationButtonText": "Get Location",
"WatchPositionButtonText": "Watch Position",
"ClearWatchPositionButtonText": "Stop Watch Position",
Expand Down
10 changes: 5 additions & 5 deletions src/BootstrapBlazor.Shared/Locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -5957,11 +5957,11 @@
"ImageViewerErrorTemplateLoadFailed": "加载失败"
},
"BootstrapBlazor.Shared.Samples.Geolocations": {
"Title": "地理定位/移动距离追踪",
"BaseUsageText": "基础用法",
"IntroText1": "通过浏览器 API 获取定位信息。",
"IntroText2": "单击按钮以获取地理位置坐标。",
"IntroText3": "注意: 出于安全考虑,当网页请求获取用户位置信息时,用户会被提示进行授权。注意不同浏览器在请求权限时有不同的策略和方式。Windows10 在未开启定位的情况下无法获取位置。",
"GeolocationsTitle": "地理定位/移动距离追踪",
"GeolocationNormalText": "基础用法",
"GeolocationNormalIntro": "通过浏览器 API 获取定位信息。",
"GeolocationNormalIntro2": "单击按钮以获取地理位置坐标。",
"GeolocationNormalIntro3": "注意: 出于安全考虑,当网页请求获取用户位置信息时,用户会被提示进行授权。注意不同浏览器在请求权限时有不同的策略和方式。Windows10 在未开启定位的情况下无法获取位置。",
"GetLocationButtonText": "获取位置",
"WatchPositionButtonText": "移动距离追踪",
"ClearWatchPositionButtonText": "停止追踪",
Expand Down
61 changes: 8 additions & 53 deletions src/BootstrapBlazor.Shared/Samples/Geolocations.razor
Original file line number Diff line number Diff line change
@@ -1,59 +1,14 @@
@page "/geolocations"
@inject IStringLocalizer<Geolocations> Localizer

<h3>@Localizer["Title"]</h3>
<h3>@Localizer["GeolocationsTitle"]</h3>

<DemoBlock Title="@Localizer["BaseUsageText"]" Introduction="@Localizer["IntroText1"]" Name="Normal">
<p>@Localizer["IntroText2"]</p>
<DemoBlock Title="@Localizer["GeolocationNormalText"]"
Introduction="@Localizer["GeolocationNormalIntro"]"
Name="Normal"
Demo="typeof(Demos.Geolocations.GeolocationNormal)">
<p>@Localizer["GeolocationNormalIntro2"]</p>
<Tips>
<p>@Localizer["IntroText3"]</p>
<p>@Localizer["GeolocationNormalIntro3"]</p>
</Tips>
@if (WatchID == 0)
{
<Button Text="@Localizer["GetLocationButtonText"]" OnClick="GetLocation"></Button>
<Button Text="@Localizer["WatchPositionButtonText"]" OnClick="WatchPosition"></Button>
}
else
{
<Button Text="@Localizer["ClearWatchPositionButtonText"]" 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="@Localizer["Longitude"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Latitude" ShowLabel="true" DisplayText="@Localizer["Latitude"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Accuracy" ShowLabel="true" DisplayText="@Localizer["Accuracy"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Altitude" ShowLabel="true" DisplayText="@Localizer["Altitude"]" />
</div>
<div class="col-12 col-sm-4">
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.AltitudeAccuracy" ShowLabel="true" DisplayText="@Localizer["AltitudeAccuracy"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Heading" ShowLabel="true" DisplayText="@Localizer["Heading"]" />
</div>
<div class="col-12 col-sm-4">
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.Speed" ShowLabel="true" DisplayText="@Localizer["Speed"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.CurrentDistance" ShowLabel="true" DisplayText="@Localizer["CurrentDistance"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.TotalDistance" ShowLabel="true" DisplayText="@Localizer["TotalDistance"]" />
</div>
<div class="col-12 col-sm-4">
<Display Value="@Model.LastUpdateTime" ShowLabel="true" DisplayText="@Localizer["LastUpdateTime"]" />
</div>
</div>
}
<BlockLogger @ref="Trace" class="mt-3" />
</DemoBlock>
109 changes: 0 additions & 109 deletions src/BootstrapBlazor.Shared/Samples/Geolocations.razor.cs

This file was deleted.

0 comments on commit bfc9531

Please sign in to comment.