Create marker on click

Back
Tags:
Marker
Example how to create a new 2D marker at the point where the map is clicked.
Usage instructions:
Add this script to map GameObject and start the scene.
Click on the map to create a new marker.
CreateMarkerOnClick.cs
/*         INFINITY CODE         */
/*   https://infinity-code.com   */

using UnityEngine;

namespace InfinityCode.OnlineMapsExamples
{
    /// <summary>
    /// Example of how to create a marker on click.
    /// </summary>
    [AddComponentMenu("Infinity Code/Online Maps/Examples (API Usage)/CreateMarkerOnClick")]
    public class CreateMarkerOnClick:MonoBehaviour
    {
        /// <summary>
        /// Reference to the map. If not specified, the current instance will be used.
        /// </summary>
        public OnlineMaps map;
        
        private void Start()
        {
            // If map is not specified, use the current instance.
            if (map == null) map = OnlineMaps.instance;
            
            // Subscribe to the click event.
            map.control.OnMapClick += OnMapClick;
        }

        private void OnMapClick()
        {
            // Get the coordinates under the cursor.
            double lng, lat;
            map.control.GetCoords(out lng, out lat);

            // Create a label for the marker.
            string label = "Marker " + (map.markerManager.Count + 1);

            // Create a new marker.
            map.markerManager.Create(lng, lat, label);
        }
    }
}