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.

CaritasServiceFundVeloKeyDataPage.razor.cs 5.8KB

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