Searches for places near the location

Back
Tags:
Google API
Math
Web services
Searches for places where you can eat in a radius of 5 km from the specified coordinates, creating markers for these places, showing them on the map, and displays the name and coordinates of these locations in the console.
Requirement: Google API Key
Usage instructions:
Add this script to map GameObject, specify your Google API Key, and start the scene.
Will be found all the food places near the specified location.
The name and location of places will be shown in the console.
FindPlacesExample.cs
/*         INFINITY CODE         */
/*   https://infinity-code.com   */

using System.Collections.Generic;
using UnityEngine;

namespace InfinityCode.OnlineMapsExamples
{
    /// <summary>
    /// Searches for places where you can eat in a radius of 5 km from the specified coordinates, creating markers for these places, showing them on the map, and displays the name and coordinates of these locations in the console.
    /// </summary>
    [AddComponentMenu("Infinity Code/Online Maps/Examples (API Usage)/FindPlacesExample")]
    public class FindPlacesExample : MonoBehaviour
    {
        /// <summary>
        /// Reference to the map. If not specified, the current instance will be used.
        /// </summary>
        public OnlineMaps map;
        
        /// <summary>
        /// Google API Key
        /// </summary>
        public string apiKey;

        private void Start()
        {
            // If the map is not specified, get the current instance.
            if (map == null) map = OnlineMaps.instance;
            
            // Makes a request to Google Places API.
            OnlineMapsGooglePlaces.FindNearby(
                apiKey,
                new OnlineMapsGooglePlaces.NearbyParams(
                    151.1957362f, // Longitude
                    -33.8670522f, // Latitude
                    5000) // Radius
                {
                    types = "food"
                }).OnComplete += OnComplete;
        }

        /// <summary>
        /// This method is called when a response is received.
        /// </summary>
        /// <param name="s">Response string</param>
        private void OnComplete(string s)
        {
            // Trying to get an array of results.
            OnlineMapsGooglePlacesResult[] results = OnlineMapsGooglePlaces.GetResults(s);

            // If there is no result
            if (results == null)
            {
                Debug.Log("Error");
                Debug.Log(s);
                return;
            }

            List<OnlineMapsMarker> markers = new List<OnlineMapsMarker>();

            foreach (OnlineMapsGooglePlacesResult result in results)
            {
                // Log name and location of each result.
                Debug.Log(result.name);
                Debug.Log(result.location);

                // Create a marker at the location of the result.
                OnlineMapsMarker marker = map.markerManager.Create(result.location, result.name);
                markers.Add(marker);
            }

            // Get center point and best zoom for markers
            Vector2 center;
            int zoom;
            OnlineMapsUtils.GetCenterPointAndZoom(markers.ToArray(), out center, out zoom);

            // Set map position and zoom.
            map.position = center;
            map.zoom = zoom + 1;
        }
    }
}