PWA Fundvelo der Caritas.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CaritasServiceFundVeloFoundKeyDataPage.razor.cs 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 static NominatimReverseAddress addressDto;
  31. private static NominatimReverseAddress AddressDto { get => addressDto; set { addressDto = value; } }
  32. public CaritasServiceFundVeloKeyDataPageBase() : base() {
  33. this.center = new LatLng(46.80121, 8.22669); // Centered on Swiss
  34. this.mapOptions = new MapOptions() {
  35. DivId = "bicycleLocationMap",
  36. Center = center,
  37. Zoom = 6,
  38. UrlTileLayer = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
  39. SubOptions = new MapSubOptions() {
  40. Attribution = "&copy; <a href='http://www.openstreetmap.org/copyright'>OpenStreetMap&nbsp;</a>",
  41. MaxZoom = 18,
  42. TileSize = 256,
  43. ZoomOffset = 0,
  44. }
  45. };
  46. this.bicycleGeoPosition = new();
  47. this.NominatimService = new NominatimService();
  48. }
  49. protected async Task AddEventsToMap() {
  50. await this.mapRef.OnClick(async (MouseEvent mouseEvent) => await OnMouseMapClicked(mouseEvent));
  51. }
  52. protected async Task InitializeMapPosition() {
  53. CreateBicycleMarkerOptions();
  54. await AddEventsToMap();
  55. if (this.bicycleGeoPosition.Latitude == 0 && this.bicycleGeoPosition.Longitude == 0) {
  56. await ShowDeviceGeoLocation();
  57. } else {
  58. await ShowBicycleGeoLocation();
  59. }
  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. }
  67. return GetFormattedAddressZipAndTown(addressDto);
  68. }
  69. protected async Task AddBicycleMarkerOnClickPosition(MouseEvent mouseEvent) {
  70. if (this.bicyclePositionMarker != null) {
  71. await bicyclePositionMarker.Remove();
  72. }
  73. this.bicyclePositionMarker = await this.MarkerFactory.CreateAndAddToMap(mouseEvent.LatLng, this.mapRef, this.bicycleMarkerOptions);
  74. }
  75. private async void CreateBicycleMarkerOptions() {
  76. this.bicycleMarkerOptions = new MarkerOptions() {
  77. IconRef = await this.IconFactory.Create(new IconOptions() {
  78. IconUrl = "./icons/bicycle_location.png",
  79. IconSize = new Point(48, 48),
  80. IconAnchor = new Point(24, 47),
  81. ShadowUrl = "./icons/bicycle_location_shadow.png",
  82. ShadowSize = new Point(48, 48),
  83. ShadowAnchor = new Point(16, 48),
  84. })
  85. };
  86. }
  87. private async Task ShowDeviceGeoLocation() {
  88. BlazorGeolocationPosition position = await this.BlazorGeolocationService.GetPositionAsync();
  89. if (position.ErrorCode != null) {
  90. Toaster.ShowError(I18n.GetString("Error.GeoLocation.Title", position.ErrorCode), I18n.GetString("Error.GeoLocation.Msg", position.ErrorMessage));
  91. } else {
  92. LatLng geoPosition = new((double)position.Latitude, (double)position.Longitude);
  93. if (this.devicePositionMarker != null) {
  94. await devicePositionMarker.Remove();
  95. }
  96. this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef);
  97. await this.mapRef.SetZoom(16);
  98. await this.mapRef.SetView(geoPosition);
  99. }
  100. }
  101. private async Task ShowBicycleGeoLocation() {
  102. if (this.bicycleGeoPosition.Latitude != 0 && this.bicycleGeoPosition.Longitude != 0) {
  103. LatLng geoPosition = new(this.bicycleGeoPosition.Latitude, this.bicycleGeoPosition.Longitude);
  104. if (this.devicePositionMarker != null) {
  105. await devicePositionMarker.Remove();
  106. }
  107. this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef);
  108. await this.mapRef.SetZoom(16);
  109. await this.mapRef.SetView(geoPosition);
  110. }
  111. }
  112. private static string GetFormattedAddressZipAndTown(NominatimReverseAddress addressDto) {
  113. string country_code = addressDto.address.country_code;
  114. string zip = SplitAndGetFirstPostcode(addressDto.address.postcode);
  115. string town = addressDto.address.city ?? addressDto.address.town ?? addressDto.address.village;
  116. return !String.IsNullOrEmpty(country_code) ? country_code.ToUpper() + "-" + zip + " " + town : zip + " " + town;
  117. }
  118. private static string SplitAndGetFirstPostcode(string postcode) {
  119. return postcode.Split("-", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)[0];
  120. }
  121. private async Task OnMouseMapClicked(MouseEvent mouseEvent) {
  122. await AddBicycleMarkerOnClickPosition(mouseEvent);
  123. this.bicycleGeoPosition.Latitude = mouseEvent.LatLng.Lat;
  124. this.bicycleGeoPosition.Longitude = mouseEvent.LatLng.Lng;
  125. addressDto = await NominatimService.GetAddressForCoordinates(mouseEvent.LatLng.Lat, mouseEvent.LatLng.Lng);
  126. if (addressDto != null) {
  127. this.bicycleGeoPosition.Address = GetFormattedAddressStreet(addressDto);
  128. this.bicycleGeoPosition.City = addressDto.address.city ?? addressDto.address.town ?? addressDto.address.village;
  129. this.bicycleGeoPosition.Zip = SplitAndGetFirstPostcode(addressDto.address.postcode);
  130. this.bicycleGeoPosition.DisplayCity = GetFormattedAddressZipAndTown(addressDto);
  131. } else {
  132. Toaster.ShowWarning(I18n.GetString("Warning.Nominatim.Title"), I18n.GetString("Warning.Nominatim.Msg"));
  133. }
  134. StateHasChanged();
  135. }
  136. private static string GetFormattedAddressStreet(NominatimReverseAddress addressDto) {
  137. string street = addressDto.address.road;
  138. string houseNr = addressDto.address.house_number ?? "";
  139. return street + (!houseNr.Equals("") ? " " + houseNr : "");
  140. }
  141. }
  142. }