PWA Fundvelo der Caritas.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CaritasServiceFundVeloFoundKeyDataPage.razor.cs 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using BlazorGeolocation;
  2. using cwebplusApp.Shared.Models;
  3. using cwebplusApp.Shared.ResourceFiles;
  4. using cwebplusApp.Shared.Services;
  5. using FisSst.BlazorMaps;
  6. using Microsoft.AspNetCore.Components;
  7. using Microsoft.Extensions.Localization;
  8. using System;
  9. using System.Threading.Tasks;
  10. namespace cwebplusApp.Pages {
  11. public class CaritasServiceFundVeloKeyDataPageBase : ComponentBase {
  12. protected readonly LatLng center;
  13. protected Map mapRef;
  14. protected MapOptions mapOptions;
  15. protected BicycleGeoPosition bicycleGeoPosition;
  16. private Marker bicyclePositionMarker;
  17. private MarkerOptions bicycleMarkerOptions;
  18. private NominatimReverseAddress addressDto;
  19. private Marker devicePositionMarker;
  20. [Inject]
  21. private IMarkerFactory MarkerFactory { get; init; }
  22. [Inject]
  23. private IIconFactory IconFactory { get; init; }
  24. [Inject]
  25. private BlazorGeolocationService BlazorGeolocationService { get; init; }
  26. [Inject]
  27. private Toaster Toaster { get; init; }
  28. [Inject]
  29. private IStringLocalizer<Resources> I18n { get; init; }
  30. private NominatimService NominatimService { get; set; }
  31. public CaritasServiceFundVeloKeyDataPageBase() : base() {
  32. this.center = new LatLng(46.80121, 8.22669); // Centered on Swiss
  33. this.mapOptions = new MapOptions() {
  34. DivId = "bicycleLocationMap",
  35. Center = center,
  36. Zoom = 6,
  37. UrlTileLayer = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
  38. SubOptions = new MapSubOptions() {
  39. Attribution = "&copy; <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap&nbsp;</a>",
  40. MaxZoom = 18,
  41. TileSize = 256,
  42. ZoomOffset = 0,
  43. }
  44. };
  45. this.bicycleGeoPosition = new();
  46. this.NominatimService = new NominatimService();
  47. }
  48. protected async Task AddEventsToMap() {
  49. await this.mapRef.OnClick(async (MouseEvent mouseEvent) => await OnMouseMapClicked(mouseEvent));
  50. }
  51. protected async Task<LatLng> InitializeDeviceMapPosition() {
  52. CreateBicycleMarkerOptions();
  53. await AddEventsToMap();
  54. return await ShowDeviceGeoLocation();
  55. }
  56. protected async Task InitializeBicycleMapPosition() {
  57. CreateBicycleMarkerOptions();
  58. await AddEventsToMap();
  59. await ShowBicycleGeoLocation();
  60. }
  61. protected async Task<string> GetFormattedAddressZipAndTown(ReportDataProvider ReportDataProvider) {
  62. if (this.addressDto == null) {
  63. this.bicycleGeoPosition.Latitude = ReportDataProvider.GetFoundReport().GeographicInfo.Latitude;
  64. this.bicycleGeoPosition.Longitude = ReportDataProvider.GetFoundReport().GeographicInfo.Longitude;
  65. this.addressDto = await NominatimService.GetAddressForCoordinates(this.bicycleGeoPosition.Latitude, this.bicycleGeoPosition.Longitude);
  66. if (this.addressDto == null) {
  67. this.addressDto = new();
  68. this.addressDto.address = new();
  69. this.addressDto.address.postcode = ReportDataProvider.GetFoundReport().GeographicInfo.Postcode;
  70. this.addressDto.address.town = ReportDataProvider.GetFoundReport().GeographicInfo.Town;
  71. }
  72. }
  73. return GetFormattedAddressZipAndTown(addressDto);
  74. }
  75. protected async Task AddBicycleMarkerOnClickPosition(MouseEvent mouseEvent) {
  76. if (this.bicyclePositionMarker != null) {
  77. await bicyclePositionMarker.Remove();
  78. }
  79. this.bicyclePositionMarker = await this.MarkerFactory.CreateAndAddToMap(mouseEvent.LatLng, this.mapRef, this.bicycleMarkerOptions);
  80. }
  81. protected async Task<LatLng> GetDeviceGeoLocation() {
  82. LatLng geoPosition;
  83. BlazorGeolocationPosition position = await this.BlazorGeolocationService.GetPositionAsync();
  84. if (position.ErrorCode != null) {
  85. Toaster.ShowError(I18n.GetString("Error.GeoLocation.Title", position.ErrorCode), I18n.GetString("Error.GeoLocation.Msg", position.ErrorMessage));
  86. geoPosition = new(0, 0);
  87. } else {
  88. geoPosition = new((double)position.Latitude, (double)position.Longitude);
  89. }
  90. return geoPosition;
  91. }
  92. private async void CreateBicycleMarkerOptions() {
  93. this.bicycleMarkerOptions = new MarkerOptions() {
  94. IconRef = await this.IconFactory.Create(new IconOptions() {
  95. IconUrl = "./icons/bicycle_location.png",
  96. IconSize = new Point(48, 48),
  97. IconAnchor = new Point(24, 47),
  98. ShadowUrl = "./icons/bicycle_location_shadow.png",
  99. ShadowSize = new Point(48, 48),
  100. ShadowAnchor = new Point(16, 48),
  101. })
  102. };
  103. }
  104. private async Task<LatLng> ShowDeviceGeoLocation() {
  105. LatLng geoPosition = await GetDeviceGeoLocation();
  106. if (this.devicePositionMarker != null) {
  107. await devicePositionMarker.Remove();
  108. }
  109. this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef);
  110. await this.mapRef.SetZoom(16);
  111. await this.mapRef.SetView(geoPosition);
  112. return geoPosition;
  113. }
  114. private async Task ShowBicycleGeoLocation() {
  115. if (this.bicycleGeoPosition.Latitude != 0 && this.bicycleGeoPosition.Longitude != 0) {
  116. LatLng geoPosition = new(this.bicycleGeoPosition.Latitude, this.bicycleGeoPosition.Longitude);
  117. await this.mapRef.SetZoom(16);
  118. await this.mapRef.SetView(geoPosition);
  119. }
  120. }
  121. private static string GetFormattedAddressZipAndTown(NominatimReverseAddress addressDto) {
  122. string country_code = addressDto.address.country_code;
  123. string zip = SplitAndGetFirstPostcode(addressDto.address.postcode);
  124. string town = addressDto.address.city ?? addressDto.address.town ?? addressDto.address.village;
  125. return (!String.IsNullOrEmpty(country_code) ? country_code.ToUpper() + "-" + zip + " " + town : zip + " " + town).TrimEnd();
  126. }
  127. private static string SplitAndGetFirstPostcode(string postcode) {
  128. return String.IsNullOrEmpty(postcode) ? "" : postcode.Split("-", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)[0];
  129. }
  130. protected async Task OnMouseMapClicked(MouseEvent mouseEvent) {
  131. await AddBicycleMarkerOnClickPosition(mouseEvent);
  132. this.bicycleGeoPosition.Latitude = mouseEvent.LatLng.Lat;
  133. this.bicycleGeoPosition.Longitude = mouseEvent.LatLng.Lng;
  134. addressDto = await NominatimService.GetAddressForCoordinates(mouseEvent.LatLng.Lat, mouseEvent.LatLng.Lng);
  135. if (addressDto != null) {
  136. this.bicycleGeoPosition.Address = GetFormattedAddressStreet(addressDto);
  137. this.bicycleGeoPosition.City = addressDto.address.city ?? addressDto.address.town ?? addressDto.address.village;
  138. this.bicycleGeoPosition.Zip = SplitAndGetFirstPostcode(addressDto.address.postcode);
  139. this.bicycleGeoPosition.DisplayCity = GetFormattedAddressZipAndTown(addressDto);
  140. } else {
  141. Toaster.ShowWarning(I18n.GetString("Warning.Nominatim.Title"), I18n.GetString("Warning.Nominatim.Msg"));
  142. }
  143. StateHasChanged();
  144. }
  145. private static string GetFormattedAddressStreet(NominatimReverseAddress addressDto) {
  146. string street = addressDto.address.road;
  147. string houseNr = addressDto.address.house_number ?? "";
  148. return street + (!houseNr.Equals("") ? " " + houseNr : "");
  149. }
  150. }
  151. }