From 8c6205df035ae63446fe355c4bf0c21d8df14ab0 Mon Sep 17 00:00:00 2001 From: Alex Chow Date: Wed, 16 Mar 2022 17:07:09 +0100 Subject: [PATCH 1/3] =?UTF-8?q?=E5=AE=8C=E5=96=84=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=8A=9F=E8=83=BD,=20=E5=AE=9E=E7=8E=B0=E6=8C=81=E7=BB=AD?= =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E8=8E=B7=E5=8F=96=E5=8D=95=E6=AC=A1=E7=A7=BB?= =?UTF-8?q?=E5=8A=A8=E8=B7=9D=E7=A6=BB/=E6=80=BB=E7=A7=BB=E5=8A=A8?= =?UTF-8?q?=E8=B7=9D=E7=A6=BB=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor.Shared/Locales/en.json | 4 +++ src/BootstrapBlazor.Shared/Locales/zh.json | 4 +++ .../Samples/Geolocations.razor | 10 +++++- .../Samples/Geolocations.razor.cs | 32 ++++++++++++++++++- .../Components/Geolocation/Geolocation.cs | 20 ++++++++++++ .../Components/Geolocation/Geolocation.js | 29 +++++++++++------ src/BootstrapBlazor/Utils/JSInterop.cs | 19 +++++++++++ .../wwwroot/js/bootstrap.blazor.bundle.min.js | 2 +- .../wwwroot/js/bootstrap.blazor.min.js | 2 +- 9 files changed, 109 insertions(+), 13 deletions(-) diff --git a/src/BootstrapBlazor.Shared/Locales/en.json b/src/BootstrapBlazor.Shared/Locales/en.json index 84c894f38..053b77616 100644 --- a/src/BootstrapBlazor.Shared/Locales/en.json +++ b/src/BootstrapBlazor.Shared/Locales/en.json @@ -2804,6 +2804,10 @@ "ClearWatchPositionButtonText": "Stop Watch Position", "GetLocationResultSuccess": "Call GetLocaltion success", "GetLocationResultFailed": "Call GetLocaltion failed", + "WatchPositionResultSuccess": "Call WatchPosition success", + "WatchPositionResultFailed": "Call WatchPosition failed", + "ClearWatchPositionResultSuccess": "Stop watch position success", + "ClearWatchPositionResultFailed": "Stop watch position failed", "Longitude": "Longitude", "Latitude": "Latitude", "Accuracy": "Accuracy", diff --git a/src/BootstrapBlazor.Shared/Locales/zh.json b/src/BootstrapBlazor.Shared/Locales/zh.json index 69a95867a..e802ca43d 100644 --- a/src/BootstrapBlazor.Shared/Locales/zh.json +++ b/src/BootstrapBlazor.Shared/Locales/zh.json @@ -2812,6 +2812,10 @@ "ClearWatchPositionButtonText": "停止追踪", "GetLocationResultSuccess": "调用 GetLocaltion 成功", "GetLocationResultFailed": "调用 GetLocaltion 失败", + "WatchPositionResultSuccess": "调用 WatchPosition 成功", + "WatchPositionResultFailed": "调用 WatchPosition 失败", + "ClearWatchPositionResultSuccess": "停止追踪成功", + "ClearWatchPositionResultFailed": "停止追踪失败", "Longitude": "经度", "Latitude": "纬度", "Accuracy": "位置精度", diff --git a/src/BootstrapBlazor.Shared/Samples/Geolocations.razor b/src/BootstrapBlazor.Shared/Samples/Geolocations.razor index c325c6e7a..7df1e587b 100644 --- a/src/BootstrapBlazor.Shared/Samples/Geolocations.razor +++ b/src/BootstrapBlazor.Shared/Samples/Geolocations.razor @@ -7,7 +7,15 @@

@Localizer["IntroText3"]

- + @if (WatchID == null) + { + + + } + else + { + + } @if (Model != null) {
diff --git a/src/BootstrapBlazor.Shared/Samples/Geolocations.razor.cs b/src/BootstrapBlazor.Shared/Samples/Geolocations.razor.cs index a2346b151..0ebaf9786 100644 --- a/src/BootstrapBlazor.Shared/Samples/Geolocations.razor.cs +++ b/src/BootstrapBlazor.Shared/Samples/Geolocations.razor.cs @@ -30,12 +30,41 @@ public partial class Geolocations : IDisposable private GeolocationItem? Model { get; set; } + /// + /// 获得/设置 获取持续定位监听器ID + /// + [Parameter] + public long? WatchID { get; set; } + private async Task GetLocation() { Interop ??= new JSInterop(JSRuntime); var ret = await Geolocation.GetLocaltion(Interop, this, nameof(GetLocationCallback)); Trace.Log(ret ? Localizer["GetLocationResultSuccess"] : Localizer["GetLocationResultFailed"]); } + private async Task WatchPosition() + { + try + { + Interop ??= new JSInterop(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 == null) return; + Interop ??= new JSInterop(JSRuntime); + var ret = await Geolocation.ClearWatchPosition(Interop, WatchID!.Value); + if (ret) WatchID = null; + Trace.Log(ret ? Localizer["ClearWatchPositionResultSuccess"] : Localizer["ClearWatchPositionResultFailed"]); + } /// /// @@ -52,12 +81,13 @@ public partial class Geolocations : IDisposable /// /// /// - protected virtual void Dispose(bool disposing) + protected virtual async void Dispose(bool disposing) { if (disposing) { if (Interop != null) { + if (WatchID != null) await Geolocation.ClearWatchPosition(Interop, WatchID!.Value); Interop.Dispose(); Interop = null; } diff --git a/src/BootstrapBlazor/Components/Geolocation/Geolocation.cs b/src/BootstrapBlazor/Components/Geolocation/Geolocation.cs index 89398553f..12ae2e72b 100644 --- a/src/BootstrapBlazor/Components/Geolocation/Geolocation.cs +++ b/src/BootstrapBlazor/Components/Geolocation/Geolocation.cs @@ -18,4 +18,24 @@ public static class Geolocation var ret = await interop.GetGeolocationItemAsync(component, callbackMethodName); return ret; } + + /// + /// 持续定位 + /// + /// + public static async Task WatchPosition(JSInterop interop, TComponent component, string callbackMethodName) where TComponent : class + { + var watchID = await interop.GetWatchPositionItemAsync(component, callbackMethodName); + return watchID; + } + + /// + /// 停止持续定位 + /// + /// + public static async Task ClearWatchPosition(JSInterop interop, long watchID) where TComponent : class + { + var ret = await interop.SetClearWatchPositionAsync(watchID); + return ret; + } } diff --git a/src/BootstrapBlazor/Components/Geolocation/Geolocation.js b/src/BootstrapBlazor/Components/Geolocation/Geolocation.js index 94ab30032..aa9254314 100644 --- a/src/BootstrapBlazor/Components/Geolocation/Geolocation.js +++ b/src/BootstrapBlazor/Components/Geolocation/Geolocation.js @@ -10,7 +10,8 @@ var deltaLatitude = (latitude2 - latitude1).toRadians(); var deltaLongitude = (longitude2 - longitude1).toRadians(); - latitude1 = latitude1.toRadians(), latitude2 = latitude2.toRadians(); + latitude1 = latitude1.toRadians(); + latitude2 = latitude2.toRadians(); var a = Math.sin(deltaLatitude / 2) * Math.sin(deltaLatitude / 2) + @@ -23,7 +24,8 @@ var d = R * c; return d; }, - bb_geo_updateLocaltion: function (position) { + bb_geo_updateLocaltion: function (position, currentDistance = 0.0, totalDistance = 0.0, lastLat, lastLong) { + // 纬度 var latitude = position.coords.latitude; // 经度 @@ -45,19 +47,16 @@ // value too large if (accuracy >= 500) { console.warn("Need more accurate values to calculate distance."); - return; } // calculate distance - var currentDistance = 0.0; - var totalDistance = 0.0; if (lastLat != null && lastLong != null) { currentDistance = $.bb_geo_distance(latitude, longitude, lastLat, lastLong); totalDistance += currentDistance; } - var lastLat = latitude; - var lastLong = longitude; + lastLat = latitude; + lastLong = longitude; if (altitude == null) { altitude = 0; @@ -116,11 +115,20 @@ } return ret; }, - bb_geo_watchPosition: function (obj) { + bb_geo_watchPosition: function (obj, method) { var id = 0; + var currentDistance = 0.0; + var totalDistance = 0.0; + var lastLat; + var lastLong; if (navigator.geolocation) { id = navigator.geolocation.watchPosition(position => { - $.bb_geo_updateLocaltion(position); + var info = $.bb_geo_updateLocaltion(position, currentDistance, totalDistance, lastLat, lastLong); + currentDistance = info.currentDistance; + totalDistance = info.totalDistance; + lastLat = info.lastLat; + lastLong = info.lastLong; + obj.invokeMethodAsync(method, info); }, $.bb_geo_handleLocationError, { maximumAge: 20000 }); @@ -131,9 +139,12 @@ return id; }, bb_geo_clearWatchLocation: function (id) { + var ret = false; if (navigator.geolocation) { + ret = true; navigator.geolocation.clearWatch(id); } + return ret; } }); })(jQuery); diff --git a/src/BootstrapBlazor/Utils/JSInterop.cs b/src/BootstrapBlazor/Utils/JSInterop.cs index d14614acb..e0b136ccc 100644 --- a/src/BootstrapBlazor/Utils/JSInterop.cs +++ b/src/BootstrapBlazor/Utils/JSInterop.cs @@ -70,6 +70,25 @@ public class JSInterop : IDisposable where TValue : class return _jsRuntime.InvokeAsync("$.bb_geo_getCurrnetPosition", _objRef, callbackMethodName); } + /// + /// + /// + /// + internal ValueTask GetWatchPositionItemAsync(TValue value, string callbackMethodName) + { + _objRef = DotNetObjectReference.Create(value); + return _jsRuntime.InvokeAsync("$.bb_geo_watchPosition", _objRef, callbackMethodName); + } + + /// + /// + /// + /// + internal ValueTask SetClearWatchPositionAsync(long watchid) + { + return _jsRuntime.InvokeAsync("$.bb_geo_clearWatchLocation", watchid); + } + /// /// diff --git a/src/BootstrapBlazor/wwwroot/js/bootstrap.blazor.bundle.min.js b/src/BootstrapBlazor/wwwroot/js/bootstrap.blazor.bundle.min.js index a6c2146be..3bf04f3b6 100644 --- a/src/BootstrapBlazor/wwwroot/js/bootstrap.blazor.bundle.min.js +++ b/src/BootstrapBlazor/wwwroot/js/bootstrap.blazor.bundle.min.js @@ -13,4 +13,4 @@ var QRCode;!function(){function a(a){this.mode=c.MODE_8BIT_BYTE,this.data=a,this /*! Summernote v0.8.18 | (c) 2013- Alan Hong and other contributors | MIT license */ !function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("jquery"));else if("function"==typeof define&&define.amd)define(["jquery"],e);else{var n="object"==typeof exports?e(require("jquery")):e(t.jQuery);for(var o in n)("object"==typeof exports?exports:t)[o]=n[o]}}(self,(function(t){return(()=>{"use strict";var e={9458:e=>{e.exports=t}},n={};function o(t){var i=n[t];if(void 0!==i)return i.exports;var r=n[t]={exports:{}};return e[t](r,r.exports,o),r.exports}o.amdO={},o.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return o.d(e,{a:e}),e},o.d=(t,e)=>{for(var n in e)o.o(e,n)&&!o.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:e[n]})},o.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),o.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var i={};return(()=>{o.r(i);var t=o(9458),e=o.n(t);function n(t){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function r(t,e){for(var n=0;n'),u=s('