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.

NominatimService.cs 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using cwebplusApp.Shared.Models;
  2. using FisSst.BlazorMaps;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Globalization;
  7. using System.Net.Http;
  8. using System.Threading.Tasks;
  9. namespace cwebplusApp.Shared.Services {
  10. public class NominatimService {
  11. public static async ValueTask<NominatimReverseAddress> GetAddressForCoordinates(double latitude, double longitude) {
  12. string lat = latitude.ToString("0.0000000000", CultureInfo.InvariantCulture);
  13. string lng = longitude.ToString("0.0000000000", CultureInfo.InvariantCulture);
  14. HttpClient httpClient = new() { BaseAddress = new Uri("https://nominatim.openstreetmap.org/") };
  15. try {
  16. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format("reverse?format=json&accept-language={0}&lat={1}&lon={2}",
  17. CultureInfo.CurrentCulture.Name, lat, lng));
  18. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  19. string contentStr = await httpResult.Content.ReadAsStringAsync();
  20. NominatimReverseAddress addressDto = JsonConvert.DeserializeObject<NominatimReverseAddress>(contentStr);
  21. return addressDto;
  22. }
  23. return null;
  24. } catch (Exception) {
  25. return null;
  26. }
  27. }
  28. public static async ValueTask<LatLng> GetCoordinatesForAddress(string addressZipCity) {
  29. HttpClient httpClient = new() { BaseAddress = new Uri("https://nominatim.openstreetmap.org/") };
  30. try {
  31. HttpResponseMessage httpResult = await httpClient.GetAsync(string.Format("search?q={0}&format=json", addressZipCity));
  32. if (httpResult.StatusCode == System.Net.HttpStatusCode.OK) {
  33. string contentStr = await httpResult.Content.ReadAsStringAsync();
  34. var settings = new JsonSerializerSettings();
  35. settings.NullValueHandling = NullValueHandling.Ignore;
  36. List<NominatimCoordinates> coordinatesDtos = JsonConvert.DeserializeObject<List<NominatimCoordinates>>(contentStr, settings);
  37. return new LatLng(coordinatesDtos[0].lat, coordinatesDtos[0].lon);
  38. }
  39. return null;
  40. } catch (Exception) {
  41. return null;
  42. }
  43. }
  44. }
  45. public class NominatimCoordinates {
  46. public double lat;
  47. public double lon;
  48. }
  49. }