PWA Fundvelo der Caritas.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CaritasServiceFundVeloFoundKeyDataPage.razor.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 partial 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. await ShowDeviceGeoLocation();
  56. }
  57. protected static string GetFormattedAddressZipAndTown() {
  58. return GetFormattedAddressZipAndTown(addressDto);
  59. }
  60. protected async Task AddBicycleMarkerOnClickPosition(MouseEvent mouseEvent) {
  61. if (this.bicyclePositionMarker != null) {
  62. await bicyclePositionMarker.Remove();
  63. }
  64. this.bicyclePositionMarker = await this.MarkerFactory.CreateAndAddToMap(mouseEvent.LatLng, this.mapRef, this.bicycleMarkerOptions);
  65. }
  66. private async void CreateBicycleMarkerOptions() {
  67. this.bicycleMarkerOptions = new MarkerOptions() {
  68. IconRef = await this.IconFactory.Create(new IconOptions() {
  69. IconUrl = "./icons/bicycle_location.png",
  70. IconSize = new Point(48, 48),
  71. IconAnchor = new Point(24, 47),
  72. ShadowUrl = "./icons/bicycle_location_shadow.png",
  73. ShadowSize = new Point(48, 48),
  74. ShadowAnchor = new Point(16, 48),
  75. })
  76. };
  77. }
  78. private async Task ShowDeviceGeoLocation() {
  79. BlazorGeolocationPosition position = await this.BlazorGeolocationService.GetPositionAsync();
  80. if (position.ErrorCode != null) {
  81. Toaster.ShowError(I18n.GetString("Error.GeoLocation.Title", position.ErrorCode), I18n.GetString("Error.GeoLocation.Msg", position.ErrorMessage));
  82. } else {
  83. LatLng geoPosition = new((double)position.Latitude, (double)position.Longitude);
  84. if (this.devicePositionMarker != null) {
  85. await devicePositionMarker.Remove();
  86. }
  87. this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef);
  88. await this.mapRef.SetZoom(16);
  89. await this.mapRef.SetView(geoPosition);
  90. }
  91. }
  92. private static string GetFormattedAddressZipAndTown(NominatimReverseAddress addressDto) {
  93. string country_code = addressDto.address.country_code;
  94. string zip = addressDto.address.postcode.Split("-", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)[0];
  95. string town = addressDto.address.village ?? addressDto.address.town ?? addressDto.address.city;
  96. return !String.IsNullOrEmpty(country_code) ? country_code.ToUpper() + "-" + zip + " " + town : zip + " " + town;
  97. }
  98. private async Task OnMouseMapClicked(MouseEvent mouseEvent) {
  99. await AddBicycleMarkerOnClickPosition(mouseEvent);
  100. this.bicycleGeoPosition.Latitude = mouseEvent.LatLng.Lat;
  101. this.bicycleGeoPosition.Longitude = mouseEvent.LatLng.Lng;
  102. addressDto = await NominatimService.GetAddressForCoordinates(mouseEvent.LatLng.Lat, mouseEvent.LatLng.Lng);
  103. if (addressDto != null) {
  104. this.bicycleGeoPosition.Address = GetFormattedAddressStreet(addressDto);
  105. this.bicycleGeoPosition.City = addressDto.address.village ?? addressDto.address.town ?? addressDto.address.city;
  106. this.bicycleGeoPosition.Zip = addressDto.address.postcode;
  107. this.bicycleGeoPosition.DisplayCity = GetFormattedAddressZipAndTown(addressDto);
  108. } else {
  109. Toaster.ShowWarning(I18n.GetString("Warning.Nominatim.Title"), I18n.GetString("Warning.Nominatim.Msg"));
  110. }
  111. StateHasChanged();
  112. }
  113. private static string GetFormattedAddressStreet(NominatimReverseAddress addressDto) {
  114. string street = addressDto.address.road;
  115. string houseNr = addressDto.address.house_number ?? "";
  116. return street + (!houseNr.Equals("") ? " " + houseNr : "");
  117. }
  118. }
  119. }