PWA Fundvelo der Caritas.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CaritasServiceFundVeloFoundKeyDataPage.razor.cs 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. LatLng geoPosition = new(46.80121, 8.22669); // Centered on Swiss
  91. if (PermissionsProvider.IsGeoLocationAllowed) {
  92. BlazorGeolocationPosition position = await this.BlazorGeolocationService.GetPositionAsync();
  93. if (position.ErrorCode != null) {
  94. Toaster.ShowError(I18n.GetString("Error.GeoLocation.Title", position.ErrorCode), I18n.GetString("Error.GeoLocation.Msg", position.ErrorMessage));
  95. } else {
  96. geoPosition = new((double)position.Latitude, (double)position.Longitude);
  97. }
  98. } else {
  99. Toaster.ShowWarning(I18n.GetString("Warning.Permission.GeoLocation.Title"), I18n.GetString("Warning.Permission.GeoLocation.Msg"));
  100. }
  101. return geoPosition;
  102. }
  103. private async void CreateBicycleMarkerOptions() {
  104. this.bicycleMarkerOptions = new MarkerOptions() {
  105. IconRef = await this.IconFactory.Create(new IconOptions() {
  106. IconUrl = "./icons/bicycle_location.png",
  107. IconSize = new Point(48, 48),
  108. IconAnchor = new Point(24, 47),
  109. ShadowUrl = "./icons/bicycle_location_shadow.png",
  110. ShadowSize = new Point(48, 48),
  111. ShadowAnchor = new Point(16, 48),
  112. })
  113. };
  114. }
  115. private async Task<LatLng> ShowDeviceGeoLocation() {
  116. LatLng geoPosition = await GetDeviceGeoLocation();
  117. if (this.devicePositionMarker != null) {
  118. await devicePositionMarker.Remove();
  119. }
  120. if (PermissionsProvider.IsGeoLocationAllowed) {
  121. this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef);
  122. await this.mapRef.SetZoom(16);
  123. } else {
  124. await this.mapRef.SetZoom(6);
  125. }
  126. await this.mapRef.SetView(geoPosition);
  127. return geoPosition;
  128. }
  129. protected async Task ShowBicycleGeoLocation(bool forceRecalc) {
  130. if (this.bicycleGeoPosition.Latitude != 0 && this.bicycleGeoPosition.Longitude != 0 && !forceRecalc) {
  131. LatLng geoPosition = new(this.bicycleGeoPosition.Latitude, this.bicycleGeoPosition.Longitude);
  132. await this.mapRef.SetZoom(16);
  133. await this.mapRef.SetView(geoPosition);
  134. } else if (!String.IsNullOrEmpty(this.bicycleGeoPosition.Address) &&
  135. !String.IsNullOrEmpty(this.bicycleGeoPosition.Zip) &&
  136. !String.IsNullOrEmpty(this.bicycleGeoPosition.City)) {
  137. LatLng geoPosition = await NominatimService.GetCoordinatesForAddress(bicycleGeoPosition.Address + ", " + bicycleGeoPosition.Zip + " " + bicycleGeoPosition.City);
  138. if (geoPosition != null) {
  139. this.bicycleGeoPosition.Latitude = geoPosition.Lat;
  140. this.bicycleGeoPosition.Longitude = geoPosition.Lng;
  141. await this.mapRef.SetZoom(16);
  142. await this.mapRef.SetView(geoPosition);
  143. MouseEvent mouseEvent = new MouseEvent();
  144. mouseEvent.LatLng = geoPosition;
  145. await AddBicycleMarkerOnClickPosition(mouseEvent);
  146. }
  147. }
  148. }
  149. private static string GetFormattedAddressZipAndTown(NominatimReverseAddress addressDto) {
  150. string zip = SplitAndGetFirstPostcode(addressDto.address.postcode);
  151. string town = addressDto.address.city ?? addressDto.address.town ?? addressDto.address.village;
  152. return (zip + " " + town).TrimEnd();
  153. }
  154. private static string SplitAndGetFirstPostcode(string postcode) {
  155. return String.IsNullOrEmpty(postcode) ? "" : postcode.Split("-", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)[0];
  156. }
  157. protected async Task OnMouseMapClicked(MouseEvent mouseEvent) {
  158. await AddBicycleMarkerOnClickPosition(mouseEvent);
  159. this.bicycleGeoPosition.Latitude = mouseEvent.LatLng.Lat;
  160. this.bicycleGeoPosition.Longitude = mouseEvent.LatLng.Lng;
  161. addressDto = await NominatimService.GetAddressForCoordinates(mouseEvent.LatLng.Lat, mouseEvent.LatLng.Lng);
  162. if (addressDto != null) {
  163. this.bicycleGeoPosition.Address = GetFormattedAddressStreet(addressDto);
  164. this.bicycleGeoPosition.City = addressDto.address.city ?? addressDto.address.town ?? addressDto.address.village;
  165. this.bicycleGeoPosition.Zip = SplitAndGetFirstPostcode(addressDto.address.postcode);
  166. this.bicycleGeoPosition.DisplayCity = GetFormattedAddressZipAndTown(addressDto);
  167. } else {
  168. Toaster.ShowWarning(I18n.GetString("Warning.Nominatim.Title"), I18n.GetString("Warning.Nominatim.Msg"));
  169. }
  170. StateHasChanged();
  171. }
  172. private static string GetFormattedAddressStreet(NominatimReverseAddress addressDto) {
  173. string street = addressDto.address.road;
  174. string houseNr = addressDto.address.house_number ?? "";
  175. return street + (!houseNr.Equals("") ? " " + houseNr : "");
  176. }
  177. }
  178. }