PWA Fundvelo der Caritas.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

CaritasServiceFundVeloFoundKeyDataPage.razor.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. [Inject]
  31. private OnlineStatusProvider OnlineStatusProvider { get; init; }
  32. [Inject]
  33. private PermissionsProvider PermissionsProvider { get; init; }
  34. private NominatimService NominatimService { get; set; }
  35. public CaritasServiceFundVeloKeyDataPageBase() : base() {
  36. this.center = new LatLng(46.80121, 8.22669); // Centered on Swiss
  37. this.mapOptions = new MapOptions() {
  38. DivId = "bicycleLocationMap",
  39. Center = center,
  40. Zoom = 6,
  41. UrlTileLayer = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
  42. SubOptions = new MapSubOptions() {
  43. Attribution = "&copy; <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap&nbsp;</a>",
  44. MaxZoom = 18,
  45. TileSize = 256,
  46. ZoomOffset = 0,
  47. }
  48. };
  49. this.bicycleGeoPosition = new();
  50. this.NominatimService = new NominatimService();
  51. }
  52. protected async Task AddEventsToMap() {
  53. await this.mapRef.OnClick(async (MouseEvent mouseEvent) => await OnMouseMapClicked(mouseEvent));
  54. }
  55. protected async Task<LatLng> InitializeDeviceMapPosition() {
  56. CreateBicycleMarkerOptions();
  57. await AddEventsToMap();
  58. return await ShowDeviceGeoLocation();
  59. }
  60. protected async Task InitializeBicycleMapPosition() {
  61. CreateBicycleMarkerOptions();
  62. await AddEventsToMap();
  63. await ShowBicycleGeoLocation(false);
  64. }
  65. protected async Task<string> GetFormattedAddressZipAndTown(ReportDataProvider ReportDataProvider) {
  66. if (this.addressDto == null) {
  67. this.bicycleGeoPosition.Latitude = ReportDataProvider.GetFoundReport().GeographicInfo.Latitude;
  68. this.bicycleGeoPosition.Longitude = ReportDataProvider.GetFoundReport().GeographicInfo.Longitude;
  69. if (OnlineStatusProvider.Online && this.bicycleGeoPosition.Latitude != 0 && this.bicycleGeoPosition.Longitude != 0) {
  70. this.addressDto = await NominatimService.GetAddressForCoordinates(this.bicycleGeoPosition.Latitude, this.bicycleGeoPosition.Longitude);
  71. }
  72. if (this.addressDto == null) {
  73. this.addressDto = new();
  74. this.addressDto.address = new();
  75. this.addressDto.address.postcode = ReportDataProvider.GetFoundReport().GeographicInfo.Postcode;
  76. this.addressDto.address.town = ReportDataProvider.GetFoundReport().GeographicInfo.Town;
  77. }
  78. }
  79. return GetFormattedAddressZipAndTown(addressDto);
  80. }
  81. protected async Task AddBicycleMarkerOnClickPosition(MouseEvent mouseEvent) {
  82. if (mouseEvent.LatLng.Lat != 0 && mouseEvent.LatLng.Lng != 0) {
  83. if (this.bicyclePositionMarker != null) {
  84. await bicyclePositionMarker.Remove();
  85. }
  86. this.bicyclePositionMarker = await this.MarkerFactory.CreateAndAddToMap(mouseEvent.LatLng, this.mapRef, this.bicycleMarkerOptions);
  87. }
  88. }
  89. protected async Task<LatLng> GetDeviceGeoLocation() {
  90. Console.WriteLine("GetDeviceGeoLocation...");
  91. LatLng geoPosition = new(46.80121, 8.22669); // Centered on Swiss
  92. if (PermissionsProvider.IsGeoLocationAllowed) {
  93. Console.WriteLine("GetDeviceGeoLocation - THEN");
  94. BlazorGeolocationPosition position = await this.BlazorGeolocationService.GetPositionAsync();
  95. if (position.ErrorCode != null) {
  96. Console.WriteLine("GetDeviceGeoLocation - THEN -ERROR");
  97. Toaster.ShowError(I18n.GetString("Error.GeoLocation.Title", position.ErrorCode), I18n.GetString("Error.GeoLocation.Msg", position.ErrorMessage));
  98. } else {
  99. geoPosition = new((double)position.Latitude, (double)position.Longitude);
  100. }
  101. } else {
  102. Console.WriteLine("GetDeviceGeoLocation - ELSE");
  103. Toaster.ShowWarning(I18n.GetString("Warning.Permission.GeoLocation.Title"), I18n.GetString("Warning.Permission.GeoLocation.Msg"));
  104. }
  105. return geoPosition;
  106. }
  107. private async void CreateBicycleMarkerOptions() {
  108. this.bicycleMarkerOptions = new MarkerOptions() {
  109. IconRef = await this.IconFactory.Create(new IconOptions() {
  110. IconUrl = "./icons/bicycle_location.png",
  111. IconSize = new Point(48, 48),
  112. IconAnchor = new Point(24, 47),
  113. ShadowUrl = "./icons/bicycle_location_shadow.png",
  114. ShadowSize = new Point(48, 48),
  115. ShadowAnchor = new Point(16, 48),
  116. })
  117. };
  118. }
  119. private async Task<LatLng> ShowDeviceGeoLocation() {
  120. LatLng geoPosition = await GetDeviceGeoLocation();
  121. if (this.devicePositionMarker != null) {
  122. await devicePositionMarker.Remove();
  123. }
  124. if (PermissionsProvider.IsGeoLocationAllowed) {
  125. this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef);
  126. await this.mapRef.SetZoom(16);
  127. } else {
  128. await this.mapRef.SetZoom(6);
  129. }
  130. await this.mapRef.SetView(geoPosition);
  131. return geoPosition;
  132. }
  133. protected async Task ShowBicycleGeoLocation(bool forceRecalc) {
  134. if (this.bicycleGeoPosition.Latitude != 0 && this.bicycleGeoPosition.Longitude != 0 && !forceRecalc) {
  135. LatLng geoPosition = new(this.bicycleGeoPosition.Latitude, this.bicycleGeoPosition.Longitude);
  136. await this.mapRef.SetZoom(16);
  137. await this.mapRef.SetView(geoPosition);
  138. } else if (!String.IsNullOrEmpty(this.bicycleGeoPosition.Address) &&
  139. !String.IsNullOrEmpty(this.bicycleGeoPosition.Zip) &&
  140. !String.IsNullOrEmpty(this.bicycleGeoPosition.City)) {
  141. LatLng geoPosition = await NominatimService.GetCoordinatesForAddress(bicycleGeoPosition.Address + ", " + bicycleGeoPosition.Zip + " " + bicycleGeoPosition.City);
  142. if (geoPosition != null) {
  143. this.bicycleGeoPosition.Latitude = geoPosition.Lat;
  144. this.bicycleGeoPosition.Longitude = geoPosition.Lng;
  145. await this.mapRef.SetZoom(16);
  146. await this.mapRef.SetView(geoPosition);
  147. MouseEvent mouseEvent = new MouseEvent();
  148. mouseEvent.LatLng = geoPosition;
  149. await AddBicycleMarkerOnClickPosition(mouseEvent);
  150. }
  151. }
  152. }
  153. private static string GetFormattedAddressZipAndTown(NominatimReverseAddress addressDto) {
  154. string zip = SplitAndGetFirstPostcode(addressDto.address.postcode);
  155. string town = addressDto.address.city ?? addressDto.address.town ?? addressDto.address.village;
  156. return (zip + " " + town).TrimEnd();
  157. }
  158. private static string SplitAndGetFirstPostcode(string postcode) {
  159. return String.IsNullOrEmpty(postcode) ? "" : postcode.Split("-", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)[0];
  160. }
  161. protected async Task OnMouseMapClicked(MouseEvent mouseEvent) {
  162. await AddBicycleMarkerOnClickPosition(mouseEvent);
  163. this.bicycleGeoPosition.Latitude = mouseEvent.LatLng.Lat;
  164. this.bicycleGeoPosition.Longitude = mouseEvent.LatLng.Lng;
  165. addressDto = await NominatimService.GetAddressForCoordinates(mouseEvent.LatLng.Lat, mouseEvent.LatLng.Lng);
  166. if (addressDto != null) {
  167. this.bicycleGeoPosition.Address = GetFormattedAddressStreet(addressDto);
  168. this.bicycleGeoPosition.City = addressDto.address.city ?? addressDto.address.town ?? addressDto.address.village;
  169. this.bicycleGeoPosition.Zip = SplitAndGetFirstPostcode(addressDto.address.postcode);
  170. this.bicycleGeoPosition.DisplayCity = GetFormattedAddressZipAndTown(addressDto);
  171. } else {
  172. Toaster.ShowWarning(I18n.GetString("Warning.Nominatim.Title"), I18n.GetString("Warning.Nominatim.Msg"));
  173. }
  174. StateHasChanged();
  175. }
  176. private static string GetFormattedAddressStreet(NominatimReverseAddress addressDto) {
  177. string street = addressDto.address.road;
  178. string houseNr = addressDto.address.house_number ?? "";
  179. return street + (!houseNr.Equals("") ? " " + houseNr : "");
  180. }
  181. }
  182. }