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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using BlazorGeolocation;
  2. using CaritasPWA.Shared.Models;
  3. using CaritasPWA.Shared.ResourceFiles;
  4. using CaritasPWA.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 CaritasPWA.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 MatBlazor.IMatToaster 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 void Show(MatBlazor.MatToastType type, string title, string message, string icon = "") {
  48. Toaster.Add(message, type, title, icon);
  49. }
  50. protected async Task AddEventsToMap() {
  51. await this.mapRef.OnClick(async (MouseEvent mouseEvent) => await OnMouseMapClicked(mouseEvent));
  52. }
  53. protected async Task InitializeMapPosition() {
  54. createBicycleMarkerOptions();
  55. await AddEventsToMap();
  56. await ShowDeviceGeoLocation();
  57. }
  58. private async void createBicycleMarkerOptions() {
  59. this.bicycleMarkerOptions = new MarkerOptions() {
  60. IconRef = await this.IconFactory.Create(new IconOptions() {
  61. IconUrl = "./icons/bicycle_location.png",
  62. IconSize = new Point(48, 48),
  63. IconAnchor = new Point(24, 47),
  64. ShadowUrl = "./icons/bicycle_location_shadow.png",
  65. ShadowSize = new Point(48, 48),
  66. ShadowAnchor = new Point(16, 48),
  67. })
  68. };
  69. }
  70. private async Task ShowDeviceGeoLocation() {
  71. BlazorGeolocationPosition position = await this.blazorGeolocationService.GetPositionAsync();
  72. if (position.ErrorCode != null) {
  73. Show(MatBlazor.MatToastType.Danger, I18n.GetString("Error.GeoLocation.Title", position.ErrorCode), I18n.GetString("Error.GeoLocation.Msg", position.ErrorMessage));
  74. } else {
  75. LatLng geoPosition = new((double)position.Latitude, (double)position.Longitude);
  76. if (this.devicePositionMarker != null) {
  77. await devicePositionMarker.Remove();
  78. }
  79. this.devicePositionMarker = await this.MarkerFactory.CreateAndAddToMap(geoPosition, this.mapRef);
  80. await this.mapRef.SetZoom(16);
  81. await this.mapRef.SetView(geoPosition);
  82. }
  83. }
  84. private async Task OnMouseMapClicked(MouseEvent mouseEvent) {
  85. await AddBicycleMarkerOnClickPosition(mouseEvent);
  86. this.bicycleGeoPosition.Latitude = mouseEvent.LatLng.Lat;
  87. this.bicycleGeoPosition.Longitude = mouseEvent.LatLng.Lng;
  88. NominatimReverseAddress addressDto = await nominatimService.GetAddressForCoordinates(mouseEvent.LatLng.Lat, mouseEvent.LatLng.Lng);
  89. if (addressDto != null) {
  90. this.bicycleGeoPosition.Address = getFormattedAddressStreet(addressDto);
  91. this.bicycleGeoPosition.City = getFormattedAddressZipAndTown(addressDto);
  92. } else {
  93. Show(MatBlazor.MatToastType.Warning, I18n.GetString("Warning.Nominatim.Title"), I18n.GetString("Warning.Nominatim.Msg"));
  94. }
  95. StateHasChanged();
  96. }
  97. private async Task AddBicycleMarkerOnClickPosition(MouseEvent mouseEvent) {
  98. if (this.bicyclePositionMarker != null) {
  99. await bicyclePositionMarker.Remove();
  100. }
  101. this.bicyclePositionMarker = await this.MarkerFactory.CreateAndAddToMap(mouseEvent.LatLng, this.mapRef, this.bicycleMarkerOptions);
  102. }
  103. private String getFormattedAddressStreet(NominatimReverseAddress addressDto) {
  104. String street = addressDto.address.road;
  105. String houseNr = addressDto.address.house_number != null ? addressDto.address.house_number : "";
  106. return street + (!houseNr.Equals("") ? " " + houseNr : "");
  107. }
  108. private String getFormattedAddressZipAndTown(NominatimReverseAddress addressDto) {
  109. String country_code = addressDto.address.country_code;
  110. String zip = addressDto.address.postcode.Split("-", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)[0];
  111. String town = addressDto.address.town;
  112. return !String.IsNullOrEmpty(country_code) ? country_code.ToUpper() + "-" + zip + " " + town : zip + " " + town;
  113. }
  114. }
  115. }