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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 Marker devicePositionMarker;
  19. [Inject]
  20. private IMarkerFactory MarkerFactory { get; init; }
  21. [Inject]
  22. private IIconFactory IconFactory { get; init; }
  23. [Inject]
  24. private BlazorGeolocationService BlazorGeolocationService { get; init; }
  25. [Inject]
  26. private Toaster Toaster { get; init; }
  27. [Inject]
  28. private IStringLocalizer<Resources> I18n { get; init; }
  29. private NominatimService NominatimService { get; set; }
  30. private NominatimReverseAddress addressDto;
  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 InitializeDeviceMapPosition() {
  52. CreateBicycleMarkerOptions();
  53. await AddEventsToMap();
  54. 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 (addressDto == null) {
  63. this.bicycleGeoPosition.Latitude = ReportDataProvider.GetFoundReport().GeographicInfo.Latitude;
  64. this.bicycleGeoPosition.Longitude = ReportDataProvider.GetFoundReport().GeographicInfo.Longitude;
  65. addressDto = await NominatimService.GetAddressForCoordinates(this.bicycleGeoPosition.Latitude, this.bicycleGeoPosition.Longitude);
  66. if (addressDto == null) {
  67. addressDto = new();
  68. addressDto.address = new();
  69. addressDto.address.postcode = ReportDataProvider.GetFoundReport().GeographicInfo.Postcode;
  70. 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. private async void CreateBicycleMarkerOptions() {
  82. this.bicycleMarkerOptions = new MarkerOptions() {
  83. IconRef = await this.IconFactory.Create(new IconOptions() {
  84. IconUrl = "./icons/bicycle_location.png",
  85. IconSize = new Point(48, 48),
  86. IconAnchor = new Point(24, 47),
  87. ShadowUrl = "./icons/bicycle_location_shadow.png",
  88. ShadowSize = new Point(48, 48),
  89. ShadowAnchor = new Point(16, 48),
  90. })
  91. };
  92. }
  93. private async Task ShowDeviceGeoLocation() {
  94. BlazorGeolocationPosition position = await this.BlazorGeolocationService.GetPositionAsync();
  95. if (position.ErrorCode != null) {
  96. Toaster.ShowError(I18n.GetString("Error.GeoLocation.Title", position.ErrorCode), I18n.GetString("Error.GeoLocation.Msg", position.ErrorMessage));
  97. } else {
  98. LatLng geoPosition = new((double)position.Latitude, (double)position.Longitude);
  99. if (this.devicePositionMarker != null) {
  100. await devicePositionMarker.Remove();
  101. }
  102. this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef);
  103. await this.mapRef.SetZoom(16);
  104. await this.mapRef.SetView(geoPosition);
  105. }
  106. }
  107. private async Task ShowBicycleGeoLocation() {
  108. if (this.bicycleGeoPosition.Latitude != 0 && this.bicycleGeoPosition.Longitude != 0) {
  109. LatLng geoPosition = new(this.bicycleGeoPosition.Latitude, this.bicycleGeoPosition.Longitude);
  110. await this.mapRef.SetZoom(16);
  111. await this.mapRef.SetView(geoPosition);
  112. }
  113. }
  114. private static string GetFormattedAddressZipAndTown(NominatimReverseAddress addressDto) {
  115. string country_code = addressDto.address.country_code;
  116. string zip = SplitAndGetFirstPostcode(addressDto.address.postcode);
  117. string town = addressDto.address.city ?? addressDto.address.town ?? addressDto.address.village;
  118. return !String.IsNullOrEmpty(country_code) ? country_code.ToUpper() + "-" + zip + " " + town : zip + " " + town;
  119. }
  120. private static string SplitAndGetFirstPostcode(string postcode) {
  121. return String.IsNullOrEmpty(postcode)? "": postcode.Split("-", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)[0];
  122. }
  123. private async Task OnMouseMapClicked(MouseEvent mouseEvent) {
  124. await AddBicycleMarkerOnClickPosition(mouseEvent);
  125. this.bicycleGeoPosition.Latitude = mouseEvent.LatLng.Lat;
  126. this.bicycleGeoPosition.Longitude = mouseEvent.LatLng.Lng;
  127. addressDto = await NominatimService.GetAddressForCoordinates(mouseEvent.LatLng.Lat, mouseEvent.LatLng.Lng);
  128. if (addressDto != null) {
  129. this.bicycleGeoPosition.Address = GetFormattedAddressStreet(addressDto);
  130. this.bicycleGeoPosition.City = addressDto.address.city ?? addressDto.address.town ?? addressDto.address.village;
  131. this.bicycleGeoPosition.Zip = SplitAndGetFirstPostcode(addressDto.address.postcode);
  132. this.bicycleGeoPosition.DisplayCity = GetFormattedAddressZipAndTown(addressDto);
  133. } else {
  134. Toaster.ShowWarning(I18n.GetString("Warning.Nominatim.Title"), I18n.GetString("Warning.Nominatim.Msg"));
  135. }
  136. StateHasChanged();
  137. }
  138. private static string GetFormattedAddressStreet(NominatimReverseAddress addressDto) {
  139. string street = addressDto.address.road;
  140. string houseNr = addressDto.address.house_number ?? "";
  141. return street + (!houseNr.Equals("") ? " " + houseNr : "");
  142. }
  143. }
  144. }