RealWorldTerrainGoogleGeocoding.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using InfinityCode.RealWorldTerrain.ExtraTypes;
  7. using InfinityCode.RealWorldTerrain.Webservices.Base;
  8. using InfinityCode.RealWorldTerrain.Webservices.Results;
  9. using InfinityCode.RealWorldTerrain.XML;
  10. using UnityEngine;
  11. namespace InfinityCode.RealWorldTerrain.Webservices
  12. {
  13. /// <summary>
  14. /// This class is used to search for a location by address.\n
  15. /// https://developers.google.com/maps/documentation/geocoding/intro
  16. /// </summary>
  17. public class RealWorldTerrainGoogleGeocoding : RealWorldTerrainTextWebServiceBase
  18. {
  19. private RealWorldTerrainGoogleGeocoding(RequestParams p)
  20. {
  21. _status = RequestStatus.downloading;
  22. StringBuilder url = new StringBuilder("https://maps.googleapis.com/maps/api/geocode/xml?sensor=false");
  23. p.GenerateURL(url);
  24. www = new RealWorldTerrainWWW(url.ToString());
  25. www.OnComplete += OnRequestComplete;
  26. }
  27. /// <summary>
  28. /// Creates a new request for a location search using object with parameters.
  29. /// </summary>
  30. /// <param name="p">Parameters of request</param>
  31. /// <returns>Instance of the search query.</returns>
  32. public static RealWorldTerrainGoogleGeocoding Find(RequestParams p)
  33. {
  34. return new RealWorldTerrainGoogleGeocoding(p);
  35. }
  36. /// <summary>
  37. /// Converts response into an array of results.
  38. /// </summary>
  39. /// <param name="response">Response of Google API.</param>
  40. /// <returns>Array of result.</returns>
  41. public static RealWorldTerrainGoogleGeocodingResult[] GetResults(string response)
  42. {
  43. try
  44. {
  45. RealWorldTerrainXML xml = RealWorldTerrainXML.Load(response);
  46. string status = xml.Find<string>("//status");
  47. if (status != "OK") return null;
  48. List<RealWorldTerrainGoogleGeocodingResult> results = new List<RealWorldTerrainGoogleGeocodingResult>();
  49. RealWorldTerrainXMLList resNodes = xml.FindAll("//result");
  50. foreach (RealWorldTerrainXML node in resNodes)
  51. {
  52. results.Add(new RealWorldTerrainGoogleGeocodingResult(node));
  53. }
  54. return results.ToArray();
  55. }
  56. catch (Exception exception)
  57. {
  58. Debug.Log(exception.Message + "\n" + exception.StackTrace);
  59. }
  60. return null;
  61. }
  62. /// <summary>
  63. /// The base class containing the request parameters.
  64. /// </summary>
  65. public abstract class RequestParams
  66. {
  67. /// <summary>
  68. /// Your application's API key. This key identifies your application for purposes of quota management.
  69. /// </summary>
  70. public string key;
  71. /// <summary>
  72. /// The language in which to return results.
  73. /// List of supported languages:
  74. /// https://developers.google.com/maps/faq#languagesupport
  75. /// </summary>
  76. public string language;
  77. /// <summary>
  78. /// Available to Google Maps APIs Premium Plan customers but not to holders of a previous Maps API for Business license.
  79. /// </summary>
  80. public string client;
  81. /// <summary>
  82. /// Uses instead of an API key to authenticate requests.
  83. /// </summary>
  84. public string signature;
  85. internal virtual void GenerateURL(StringBuilder url)
  86. {
  87. if (!string.IsNullOrEmpty(key)) url.Append("&key=").Append(key);
  88. if (!string.IsNullOrEmpty(language)) url.Append("&language=").Append(language);
  89. if (!string.IsNullOrEmpty(client)) url.Append("&client=").Append(client);
  90. if (!string.IsNullOrEmpty(signature)) url.Append("&signature=").Append(signature);
  91. }
  92. }
  93. /// <summary>
  94. /// Request parameters for Geocoding
  95. /// </summary>
  96. public class GeocodingParams : RequestParams
  97. {
  98. /// <summary>
  99. /// The street address that you want to geocode, in the format used by the national postal service of the country concerned. \n
  100. /// Additional address elements such as business names and unit, suite or floor numbers should be avoided.
  101. /// </summary>
  102. public string address;
  103. /// <summary>
  104. /// A component filter for which you wish to obtain a geocode. \n
  105. /// See Component Filtering for more information. \n
  106. /// https://developers.google.com/maps/documentation/geocoding/intro?hl=en#ComponentFiltering \n
  107. /// The components filter will also be accepted as an optional parameter if an address is provided.
  108. /// </summary>
  109. public string components;
  110. /// <summary>
  111. /// The bounding box of the viewport within which to bias geocode results more prominently. \n
  112. /// This parameter will only influence, not fully restrict, results from the geocoder.
  113. /// </summary>
  114. public RealWorldTerrainGeoRect bounds;
  115. /// <summary>
  116. /// The region code, specified as a ccTLD ("top-level domain") two-character value. \n
  117. /// This parameter will only influence, not fully restrict, results from the geocoder.
  118. /// </summary>
  119. public string region;
  120. /// <summary>
  121. /// Constructor
  122. /// </summary>
  123. public GeocodingParams()
  124. {
  125. }
  126. /// <summary>
  127. /// Constructor
  128. /// </summary>
  129. /// <param name="address">
  130. /// The street address that you want to geocode, in the format used by the national postal service of the country concerned. \n
  131. /// Additional address elements such as business names and unit, suite or floor numbers should be avoided.
  132. /// </param>
  133. public GeocodingParams(string address)
  134. {
  135. this.address = address;
  136. }
  137. internal override void GenerateURL(StringBuilder url)
  138. {
  139. base.GenerateURL(url);
  140. if (!string.IsNullOrEmpty(address)) url.Append("&address=").Append(RealWorldTerrainWWW.EscapeURL(address));
  141. if (!string.IsNullOrEmpty(components)) url.Append("&components=").Append(components);
  142. if (bounds != null) url.Append("&bounds=").Append(bounds.bottom).Append(",").Append(bounds.left).Append("|").Append(bounds.top).Append(",").Append(bounds.right);
  143. if (!string.IsNullOrEmpty(region)) url.Append("&region=").Append(region);
  144. }
  145. }
  146. /// <summary>
  147. /// Request parameters for Reverse Geocoding
  148. /// </summary>
  149. public class ReverseGeocodingParams : RequestParams
  150. {
  151. /// <summary>
  152. /// The longitude value specifying the location for which you wish to obtain the closest, human-readable address.
  153. /// </summary>
  154. public double? longitude;
  155. /// <summary>
  156. /// The latitude value specifying the location for which you wish to obtain the closest, human-readable address.
  157. /// </summary>
  158. public double? latitude;
  159. /// <summary>
  160. /// The place ID of the place for which you wish to obtain the human-readable address. \n
  161. /// The place ID is a unique identifier that can be used with other Google APIs. \n
  162. /// For example, you can use the placeID returned by the Google Maps Roads API to get the address for a snapped point. \n
  163. /// For more information about place IDs, see the place ID overview. \n
  164. /// The place ID may only be specified if the request includes an API key or a Google Maps APIs Premium Plan client ID.
  165. /// </summary>
  166. public string placeId;
  167. /// <summary>
  168. /// One or more address types, separated by a pipe (|). \n
  169. /// Examples of address types: country, street_address, postal_code. \n
  170. /// For a full list of allowable values, see the address types on this page:\n
  171. /// https://developers.google.com/maps/documentation/geocoding/intro?hl=en#Types \n
  172. /// Specifying a type will restrict the results to this type. \n
  173. /// If multiple types are specified, the API will return all addresses that match any of the types. \n
  174. /// Note: This parameter is available only for requests that include an API key or a client ID.
  175. /// </summary>
  176. public string result_type;
  177. /// <summary>
  178. /// One or more location types, separated by a pipe (|). \n
  179. /// https://developers.google.com/maps/documentation/geocoding/intro?hl=en#ReverseGeocoding \n
  180. /// Specifying a type will restrict the results to this type. \n
  181. /// If multiple types are specified, the API will return all addresses that match any of the types. \n
  182. /// Note: This parameter is available only for requests that include an API key or a client ID.
  183. /// </summary>
  184. public string location_type;
  185. /// <summary>
  186. /// The longitude and latitude values specifying the location for which you wish to obtain the closest, human-readable address.
  187. /// </summary>
  188. public Vector2? location
  189. {
  190. get
  191. {
  192. return new Vector2(longitude.HasValue ? (float)longitude.Value : 0, latitude.HasValue ? (float)latitude.Value : 0);
  193. }
  194. set
  195. {
  196. if (value.HasValue)
  197. {
  198. longitude = value.Value.x;
  199. latitude = value.Value.y;
  200. }
  201. else
  202. {
  203. longitude = null;
  204. latitude = null;
  205. }
  206. }
  207. }
  208. /// <summary>
  209. /// Constructor
  210. /// </summary>
  211. /// <param name="longitude">The longitude value specifying the location for which you wish to obtain the closest, human-readable address. </param>
  212. /// <param name="latitude">The latitude value specifying the location for which you wish to obtain the closest, human-readable address. </param>
  213. public ReverseGeocodingParams(double longitude, double latitude)
  214. {
  215. this.longitude = longitude;
  216. this.latitude = latitude;
  217. }
  218. /// <summary>
  219. /// Constructor
  220. /// </summary>
  221. /// <param name="location">The longitude and latitude values specifying the location for which you wish to obtain the closest, human-readable address. </param>
  222. public ReverseGeocodingParams(Vector2 location)
  223. {
  224. this.location = location;
  225. }
  226. /// <summary>
  227. /// Constructor
  228. /// </summary>
  229. /// <param name="placeId">The place ID of the place for which you wish to obtain the human-readable address. \n
  230. /// The place ID is a unique identifier that can be used with other Google APIs. \n
  231. /// For example, you can use the placeID returned by the Google Maps Roads API to get the address for a snapped point. \n
  232. /// For more information about place IDs, see the place ID overview. \n
  233. /// The place ID may only be specified if the request includes an API key or a Google Maps APIs Premium Plan client ID.
  234. /// </param>
  235. public ReverseGeocodingParams(string placeId)
  236. {
  237. this.placeId = placeId;
  238. }
  239. internal override void GenerateURL(StringBuilder url)
  240. {
  241. base.GenerateURL(url);
  242. if (longitude.HasValue && latitude.HasValue) url.Append("&latlng=").Append(latitude.Value.ToString(RealWorldTerrainCultureInfo.numberFormat)).Append(",").Append(longitude.Value.ToString(RealWorldTerrainCultureInfo.numberFormat));
  243. else if (!string.IsNullOrEmpty(placeId)) url.Append("&placeId=").Append(placeId);
  244. else throw new Exception("You must specify latitude and longitude, location, or placeId.");
  245. if (!string.IsNullOrEmpty(result_type)) url.Append("&result_type=").Append(result_type);
  246. if (!string.IsNullOrEmpty(location_type)) url.Append("&location_type=").Append(location_type);
  247. }
  248. }
  249. }
  250. }