Bing Maps Location web service

Back
Tags:
Web services
Example of a request to Bing Maps Location API web service, and get the result.
Usage instructions:
Add this script to map GameObject, enter your Bing Maps API Key and start the scene.
Will be found all "Moscow" and log formatted addresses.
By clicking on the map, will be found the name of the location under the cursor and log the formatted address.
BingMapsLocationAPIExample.cs
/*         INFINITY CODE         */
/*   https://infinity-code.com   */

using System.Collections.Generic;
using OnlineMaps;
using OnlineMaps.Webservices;
using UnityEngine;

namespace OnlineMapsExamples
{
    /// <summary>
    /// Example of a request to Bing Maps Location API, and get the result.
    /// </summary>
    [AddComponentMenu(Utils.ExampleMenuPath + "BingMapsLocationAPIExample")]
    public class BingMapsLocationAPIExample : MonoBehaviour
    {
        /// <summary>
        /// Reference to the map. If not specified, the current instance will be used.
        /// </summary>
        public Map map;

        /// <summary>
        /// Search query
        /// </summary>
        public string query = "New York";

        private void Start()
        {
            if (!KeyManager.hasBingMaps)
            {
                Debug.LogError("Bing Maps API Key is missing. Specify the key in Key Manager.");
                return;
            }
            
            // If map is not specified, use the current instance.
            if (!map && !(map = Map.instance))
            {
                Debug.LogError("Map not found");
                return;
            }
            
            // Looking for a location by name.
            new BingMapsGeocodingRequest(query)
                .HandleResult(OnResult) // Subscribe to OnResult event
                .Send(); // Send the request

            // Subscribe to map click event.
            map.control.OnClick += OnMapClick;
        }

        /// <summary>
        /// This method is called when click on map.
        /// </summary>
        private void OnMapClick()
        {
            // Looking for a location by coordinates.
            new BingMapsReverseGeocodingRequest(map.view.center)
                .HandleResult(OnResult) // Subscribe to OnResult event
                .Send(); // Send the request
        }

        /// <summary>
        /// This method is called when a response is received and results are processed.
        /// </summary>
        /// <param name="results">Array of results</param>
        private static void OnResult(BingMapsLocationResult[] results)
        {
            if (results == null)
            {
                Debug.Log("No results");
                return;
            }

            // Log results info.
            Debug.Log(results.Length);
            foreach (BingMapsLocationResult result in results)
            {
                Debug.Log(result.name);
                Debug.Log(result.formattedAddress);
                foreach (KeyValuePair<string, string> pair in result.address)
                {
                    Debug.Log(pair.Key + ": " + pair.Value);
                }
                Debug.Log("------------------------------");
            }
        }
    }
}