RealWorldTerrainPlacesResult.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System;
  4. using System.Collections.Generic;
  5. using InfinityCode.RealWorldTerrain.XML;
  6. using UnityEngine;
  7. namespace InfinityCode.RealWorldTerrain.Webservices.Results
  8. {
  9. /// <summary>
  10. /// Result of Google Maps Places query.
  11. /// </summary>
  12. public class RealWorldTerrainPlacesResult
  13. {
  14. /// <summary>
  15. /// Coordinates of the place.
  16. /// </summary>
  17. public Vector2 location;
  18. /// <summary>
  19. /// URL of a recommended icon which may be displayed to the user when indicating this result.
  20. /// </summary>
  21. public string icon;
  22. /// <summary>
  23. /// Unique stable identifier denoting this place. \n
  24. /// This identifier may not be used to retrieve information about this place, but is guaranteed to be valid across sessions. \n
  25. /// It can be used to consolidate data about this place, and to verify the identity of a place across separate searches. \n
  26. /// Note: The id is now deprecated in favor of place_id.
  27. /// </summary>
  28. public string id;
  29. /// <summary>
  30. /// Human-readable address of this place. \n
  31. /// Often this address is equivalent to the "postal address". \n
  32. /// The formatted_address property is only returned for a Text Search.
  33. /// </summary>
  34. public string formatted_address;
  35. /// <summary>
  36. /// Human-readable name for the returned result. \n
  37. /// For establishment results, this is usually the business name.
  38. /// </summary>
  39. public string name;
  40. /// <summary>
  41. /// Unique identifier for a place.
  42. /// </summary>
  43. public string place_id;
  44. /// <summary>
  45. /// Unique token that you can use to retrieve additional information about this place in a Place Details request. \n
  46. /// Although this token uniquely identifies the place, the converse is not true. \n
  47. /// A place may have many valid reference tokens. \n
  48. /// It's not guaranteed that the same token will be returned for any given place across different searches. \n
  49. /// Note: The reference is now deprecated in favor of place_id.
  50. /// </summary>
  51. public string reference;
  52. /// <summary>
  53. /// Array of feature types describing the given result. \n
  54. /// XML responses include multiple type elements if more than one type is assigned to the result.
  55. /// </summary>
  56. public string[] types;
  57. /// <summary>
  58. /// Feature name of a nearby location. \n
  59. /// Often this feature refers to a street or neighborhood within the given results. \n
  60. /// The vicinity property is only returned for a Nearby Search.
  61. /// </summary>
  62. public string vicinity;
  63. /// <summary>
  64. /// The price level of the place, on a scale of 0 to 4. \n
  65. /// The exact amount indicated by a specific value will vary from region to region. \n
  66. /// Price levels are interpreted as follows: \n
  67. /// -1 - Unknown \n
  68. /// 0 - Free \n
  69. /// 1 - Inexpensive \n
  70. /// 2 - Moderate \n
  71. /// 3 - Expensive \n
  72. /// 4 - Very Expensive
  73. /// </summary>
  74. public int price_level = -1;
  75. /// <summary>
  76. /// Place's rating, from 1.0 to 5.0, based on aggregated user reviews.
  77. /// </summary>
  78. public float rating;
  79. /// <summary>
  80. /// Value indicating if the place is open at the current time.
  81. /// </summary>
  82. public bool open_now;
  83. /// <summary>
  84. /// Indicates the scope of the place_id.
  85. /// </summary>
  86. public string scope;
  87. /// <summary>
  88. /// Undocumented in Google Maps Places API.
  89. /// </summary>
  90. public string[] weekday_text;
  91. /// <summary>
  92. /// Array of photo objects, each containing a reference to an image. \n
  93. /// A Place Search will return at most one photo object. \n
  94. /// Performing a Place Details request on the place may return up to ten photos.
  95. /// </summary>
  96. public Photo[] photos;
  97. public RealWorldTerrainPlacesResult()
  98. {
  99. }
  100. /// <summary>
  101. /// Constructor
  102. /// </summary>
  103. /// <param name="node">Place node from response</param>
  104. public RealWorldTerrainPlacesResult(RealWorldTerrainXML node)
  105. {
  106. List<Photo> photos = new List<Photo>();
  107. List<string> types = new List<string>();
  108. List<string> weekday_text = new List<string>();
  109. foreach (RealWorldTerrainXML n in node)
  110. {
  111. if (n.name == "name") name = n.Value();
  112. else if (n.name == "id") id = n.Value();
  113. else if (n.name == "vicinity") vicinity = n.Value();
  114. else if (n.name == "type") types.Add(n.Value());
  115. else if (n.name == "geometry") location = RealWorldTerrainXML.GetVector2FromNode(n[0]);
  116. else if (n.name == "rating") rating = n.Value<float>();
  117. else if (n.name == "icon") icon = n.Value();
  118. else if (n.name == "reference") reference = n.Value();
  119. else if (n.name == "place_id") place_id = n.Value();
  120. else if (n.name == "scope") scope = n.Value();
  121. else if (n.name == "price_level") price_level = n.Value<int>();
  122. else if (n.name == "formatted_address") formatted_address = n.Value();
  123. else if (n.name == "opening_hours")
  124. {
  125. open_now = n.Get<string>("open_now") == "true";
  126. foreach (RealWorldTerrainXML wdt in n.FindAll("weekday_text")) weekday_text.Add(wdt.Value());
  127. }
  128. else if (n.name == "photo")
  129. {
  130. photos.Add(new Photo(n));
  131. }
  132. else Debug.Log(n.name);
  133. }
  134. this.photos = photos.ToArray();
  135. this.types = types.ToArray();
  136. this.weekday_text = weekday_text.ToArray();
  137. }
  138. /// <summary>
  139. /// Photo objects, contains a reference to an image.
  140. /// </summary>
  141. public class Photo
  142. {
  143. /// <summary>
  144. /// The maximum width of the image.
  145. /// </summary>
  146. public int width;
  147. /// <summary>
  148. /// The maximum height of the image.
  149. /// </summary>
  150. public int height;
  151. /// <summary>
  152. /// String used to identify the photo when you perform a Photo request.
  153. /// </summary>
  154. public string photo_reference;
  155. /// <summary>
  156. /// Contains any required attributions. This field will always be present, but may be empty.
  157. /// </summary>
  158. public string[] html_attributions;
  159. public Photo()
  160. {
  161. }
  162. /// <summary>
  163. /// Constructor
  164. /// </summary>
  165. /// <param name="node">Photo node from response</param>
  166. public Photo(RealWorldTerrainXML node)
  167. {
  168. try
  169. {
  170. width = node.Get<int>("width");
  171. height = node.Get<int>("height");
  172. photo_reference = node["photo_reference"].Value();
  173. List<string> html_attributions = new List<string>();
  174. foreach (RealWorldTerrainXML ha in node.FindAll("html_attributions")) html_attributions.Add(ha.Value());
  175. this.html_attributions = html_attributions.ToArray();
  176. }
  177. catch (Exception)
  178. {
  179. }
  180. }
  181. /// <summary>
  182. /// Download photo from Google Places.
  183. /// </summary>
  184. /// <param name="key">Google Maps API Key.</param>
  185. /// <param name="maxWidth">
  186. /// Specifies the maximum desired width, in pixels, of the image returned by the Place Photos service. \n
  187. /// If the image is smaller than the values specified, the original image will be returned. \n
  188. /// If the image is larger in either dimension, it will be scaled to match the smaller of the two dimensions, restricted to its original aspect ratio. \n
  189. /// maxWidth accept an integer between 1 and 1600.
  190. /// </param>
  191. /// <param name="maxHeight">
  192. /// Specifies the maximum desired height, in pixels, of the image returned by the Place Photos service. \n
  193. /// If the image is smaller than the values specified, the original image will be returned. \n
  194. /// If the image is larger in either dimension, it will be scaled to match the smaller of the two dimensions, restricted to its original aspect ratio. \n
  195. /// maxHeight accept an integer between 1 and 1600.\n
  196. /// </param>
  197. /// <returns></returns>
  198. public RealWorldTerrainGooglePlacePhoto Download(string key, int? maxWidth = null, int? maxHeight = null)
  199. {
  200. if (!maxWidth.HasValue) maxWidth = width;
  201. if (!maxHeight.HasValue) maxHeight = height;
  202. return RealWorldTerrainGooglePlacePhoto.Download(key, photo_reference, maxWidth, maxHeight);
  203. }
  204. }
  205. }
  206. }