RealWorldTerrainGooglePlacePhoto.cs 4.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* INFINITY CODE 2013-2019 */
  2. /* http://www.infinity-code.com */
  3. using System;
  4. using System.Text;
  5. using InfinityCode.RealWorldTerrain.ExtraTypes;
  6. using UnityEngine;
  7. namespace InfinityCode.RealWorldTerrain.Webservices
  8. {
  9. /// <summary>
  10. /// The Place Photo service, part of the Google Places API Web Service, is a read-only API that allows you to add high quality photographic content to your application. \n
  11. /// The Place Photo service gives you access to the millions of photos stored in the Places and Google+ Local database. \n
  12. /// When you get place information using a Place Details request, photo references will be returned for relevant photographic content. \n
  13. /// The Nearby Search and Text Search requests also return a single photo reference per place, when relevant. \n
  14. /// Using the Photo service you can then access the referenced photos and resize the image to the optimal size for your application.
  15. /// </summary>
  16. public class RealWorldTerrainGooglePlacePhoto : RealWorldTerrainWebServiceBase
  17. {
  18. /// <summary>
  19. /// Event that occurs when a response is received from Google API.
  20. /// </summary>
  21. public Action<Texture2D> OnComplete;
  22. private RealWorldTerrainGooglePlacePhoto(string key, string photo_reference, int? maxWidth, int? maxHeight)
  23. {
  24. StringBuilder builder = new StringBuilder("https://maps.googleapis.com/maps/api/place/photo?key=").Append(key);
  25. builder.Append("&photo_reference=").Append(photo_reference);
  26. if (maxWidth.HasValue) builder.Append("&maxwidth=").Append(maxWidth);
  27. if (maxHeight.HasValue) builder.Append("&maxheight=").Append(maxHeight);
  28. if (!maxWidth.HasValue && !maxHeight.HasValue) builder.Append("&maxwidth=").Append(800);
  29. www = new RealWorldTerrainWWW(builder.ToString());
  30. www.OnComplete += OnRequestComplete;
  31. }
  32. private void OnRequestComplete(RealWorldTerrainWWW www)
  33. {
  34. if (www != null && www.isDone)
  35. {
  36. _status = string.IsNullOrEmpty(www.error) ? RequestStatus.success : RequestStatus.error;
  37. if (OnComplete != null)
  38. {
  39. if (_status == RequestStatus.success)
  40. {
  41. Texture2D texture = new Texture2D(1, 1);
  42. www.LoadImageIntoTexture(texture);
  43. OnComplete(texture);
  44. }
  45. else OnComplete(null);
  46. }
  47. if (OnFinish != null) OnFinish(this);
  48. _status = RequestStatus.disposed;
  49. customData = null;
  50. this.www = null;
  51. }
  52. }
  53. /// <summary>
  54. /// Download photo from Google Places.
  55. /// </summary>
  56. /// <param name="key">Google Maps API Key</param>
  57. /// <param name="photo_reference">String used to identify the photo when you perform a Photo request.</param>
  58. /// <param name="maxWidth">
  59. /// Specifies the maximum desired width, in pixels, of the image returned by the Place Photos service. \n
  60. /// If the image is smaller than the values specified, the original image will be returned. \n
  61. /// 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
  62. /// maxWidth accept an integer between 1 and 1600.
  63. /// </param>
  64. /// <param name="maxHeight">
  65. /// Specifies the maximum desired height, in pixels, of the image returned by the Place Photos service. \n
  66. /// If the image is smaller than the values specified, the original image will be returned. \n
  67. /// 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
  68. /// maxHeight accept an integer between 1 and 1600.\n
  69. /// </param>
  70. /// <returns></returns>
  71. public static RealWorldTerrainGooglePlacePhoto Download(string key, string photo_reference, int? maxWidth = null, int? maxHeight = null)
  72. {
  73. return new RealWorldTerrainGooglePlacePhoto(key, photo_reference, maxWidth, maxHeight);
  74. }
  75. public override void Destroy()
  76. {
  77. if (OnDispose != null) OnDispose(this);
  78. www = null;
  79. _status = RequestStatus.disposed;
  80. customData = null;
  81. OnComplete = null;
  82. OnFinish = null;
  83. }
  84. }
  85. }