GoogleGeocodingService.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using GuigleApi;
  2. using GuigleApi.Models.Extension;
  3. using GuigleApi.Models.Response;
  4. using Microsoft.EntityFrameworkCore.Internal;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Net.Http;
  9. using System.Text;
  10. namespace GreenTree.Strohrmann.ERP.Services.Geolocator
  11. {
  12. public class GoogleGeocodingService : IGeocodingService
  13. {
  14. #region Contants
  15. // The default address component types to check for an address validation
  16. private readonly string[] _defaultCheckComponents =
  17. {
  18. "street_number",
  19. "route",
  20. "locality",
  21. "country",
  22. "postal_code"
  23. };
  24. #endregion
  25. #region DI fields
  26. // The global Google API settings
  27. private readonly GoogleApiOptions _googleApiOptions;
  28. #endregion
  29. #region Ctor
  30. /// <summary>
  31. /// Initializes a new instance of the GoogleGeocodingService
  32. /// </summary>
  33. /// <param name="googleApiOptions">Global Google API options.</param>
  34. public GoogleGeocodingService(
  35. GoogleApiOptions googleApiOptions)
  36. {
  37. _googleApiOptions = googleApiOptions;
  38. }
  39. #endregion
  40. #region Implementation
  41. /// <summary>
  42. /// Checks an address search string for valid street address
  43. /// </summary>
  44. /// <param name="searchString">The address search string.</param>
  45. public bool IsValidAddress(string searchString)
  46. {
  47. return IsValidAddress(searchString, _defaultCheckComponents);
  48. }
  49. /// <summary>
  50. /// Checks an address search string for valid street address
  51. /// </summary>
  52. /// <param name="searchString">The address search string.</param>
  53. /// <param name="addressComponents">
  54. /// The address components the API result shall be checked for
  55. /// (NULL if validation check shall succeed).
  56. /// </param>
  57. public bool IsValidAddress(string searchString, string[] addressComponents)
  58. {
  59. if (!_googleApiOptions.Enabled)
  60. return true;
  61. if (addressComponents == null)
  62. return true;
  63. var client = new HttpClient();
  64. var googleGeocodingApi = new GoogleGeocodingApi(_googleApiOptions.ApiKey);
  65. var addressSearchResult = googleGeocodingApi.SearchAddress(
  66. client, searchString);
  67. var result = addressSearchResult.Result;
  68. if (result.Status != "OK")
  69. return false;
  70. if (result.Results.Count == 0)
  71. return false;
  72. var addressResult = result.Results[0];
  73. foreach (var addressComponent in addressComponents)
  74. {
  75. if (!addressResult.AddressComponents.Exists(a => a.Types.Any(t => t.ToString() == addressComponent)))
  76. return false;
  77. }
  78. return true;
  79. }
  80. #endregion
  81. }
  82. }