RealWorldTerrainGooglePlaceDetails.cs 4.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System.Text;
  4. using InfinityCode.RealWorldTerrain.ExtraTypes;
  5. using InfinityCode.RealWorldTerrain.Webservices.Base;
  6. using InfinityCode.RealWorldTerrain.Webservices.Results;
  7. using InfinityCode.RealWorldTerrain.XML;
  8. namespace InfinityCode.RealWorldTerrain.Webservices
  9. {
  10. /// <summary>
  11. /// A Place Details request returns more comprehensive information about the indicated place such as its complete address, phone number, user rating and reviews.\n
  12. /// <strong>Requires Google Maps API key.</strong>\n
  13. /// https://developers.google.com/places/webservice/details
  14. /// </summary>
  15. public class RealWorldTerrainGooglePlaceDetails : RealWorldTerrainTextWebServiceBase
  16. {
  17. protected RealWorldTerrainGooglePlaceDetails(string key, string place_id, string reference, string language)
  18. {
  19. _status = RequestStatus.downloading;
  20. StringBuilder url = new StringBuilder("https://maps.googleapis.com/maps/api/place/details/xml?sensor=false&key=").Append(key);
  21. if (!string.IsNullOrEmpty(place_id)) url.Append("&placeid=").Append(place_id);
  22. if (!string.IsNullOrEmpty(reference)) url.Append("&reference=").Append(reference);
  23. if (!string.IsNullOrEmpty(language)) url.Append("&language=").Append(language);
  24. www = new RealWorldTerrainWWW(url.ToString());
  25. www.OnComplete += OnRequestComplete;
  26. }
  27. /// <summary>
  28. /// Gets details about the place by place_id.
  29. /// </summary>
  30. /// <param name="key">
  31. /// Your application's API key.\n
  32. /// This key identifies your application for purposes of quota management and so that places added from your application are made immediately available to your app.\n
  33. /// Visit the Google Developers Console to create an API Project and obtain your key.
  34. /// </param>
  35. /// <param name="place_id">A textual identifier that uniquely identifies a place, returned from a Place Search.</param>
  36. /// <param name="language">
  37. /// The language code, indicating in which language the results should be returned, if possible.\n
  38. /// Note that some fields may not be available in the requested language.
  39. /// </param>
  40. /// <returns>Query instance to the Google API.</returns>
  41. public static RealWorldTerrainGooglePlaceDetails FindByPlaceID(string key, string place_id, string language = null)
  42. {
  43. return new RealWorldTerrainGooglePlaceDetails(key, place_id, null, language);
  44. }
  45. /// <summary>
  46. /// Gets details about the place by reference.
  47. /// </summary>
  48. /// <param name="key">
  49. /// Your application's API key. \n
  50. /// This key identifies your application for purposes of quota management and so that places added from your application are made immediately available to your app.\n
  51. /// Visit the Google Developers Console to create an API Project and obtain your key.
  52. /// </param>
  53. /// <param name="reference">
  54. /// A textual identifier that uniquely identifies a place, returned from a Place Search.\n
  55. /// Note: The reference is now deprecated in favor of placeid.
  56. /// </param>
  57. /// <param name="language">
  58. /// The language code, indicating in which language the results should be returned, if possible.\n
  59. /// Note that some fields may not be available in the requested language.
  60. /// </param>
  61. /// <returns>Query instance to the Google API.</returns>
  62. public static RealWorldTerrainGooglePlaceDetails FindByReference(string key, string reference, string language = null)
  63. {
  64. return new RealWorldTerrainGooglePlaceDetails(key, null, reference, language);
  65. }
  66. /// <summary>
  67. /// Converts response into an result object.
  68. /// Note: The object may not contain all the available fields.\n
  69. /// Other fields can be obtained from RealWorldTerrainGooglePlaceDetailsResult.node.
  70. /// </summary>
  71. /// <param name="response">Response of Google API.</param>
  72. /// <returns>Result object or null.</returns>
  73. public static RealWorldTerrainGooglePlaceDetailsResult GetResult(string response)
  74. {
  75. try
  76. {
  77. RealWorldTerrainXML xml = RealWorldTerrainXML.Load(response);
  78. string status = xml.Find<string>("//status");
  79. if (status != "OK") return null;
  80. return new RealWorldTerrainGooglePlaceDetailsResult(xml["result"]);
  81. }
  82. catch
  83. {
  84. }
  85. return null;
  86. }
  87. }
  88. }