Topic: MapBox Search

Hi guys,
Does anyone has an example for search using Mapbox geocoding?
I tried to edit SearchPanel script, but I think I am doing it wrong.

I found attached script on Mapbox webpage. But not sure how to transfer it to Online Maps.

Thank you for help

public class ForwardGeocodeUserSearch : MonoBehaviour
    {
        TMP_InputField _inputField;

        ForwardGeocodeResource _resource;

        Vector2d _coordinate;
        public Vector2d Coordinate
        {
            get
            {
                return _coordinate;
            }
        }

        bool _hasResponse;
        public bool HasResponse
        {
            get
            {
                return _hasResponse;
            }
        }

        public ForwardGeocodeResponse Response { get; private set; }

        public event Action<ForwardGeocodeResponse> OnGeocoderResponse = delegate { };

        void Awake()
        {
            _inputField = GetComponent<TMP_InputField>();
            _inputField.onEndEdit.AddListener(HandleUserInput);
            _resource = new ForwardGeocodeResource("");
        }

        void HandleUserInput(string searchString)
        {
            _hasResponse = false;
            if (!string.IsNullOrEmpty(searchString))
            {
                _resource.Query = searchString;
                MapboxAccess.Instance.Geocoder.Geocode(_resource, HandleGeocoderResponse);
            }
        }

        void HandleGeocoderResponse(ForwardGeocodeResponse res)
        {
            _hasResponse = true;
            if (null == res)
            {
                _inputField.text = "no geocode response";
            }
            else if (null != res.Features && res.Features.Count > 0)
            {
                var center = res.Features[0].Center;
                _coordinate = res.Features[0].Center;
            }
            Response = res;
            OnGeocoderResponse(res);
        }
    }

Re: MapBox Search

Hello.

Actually, it's very easy.
Example:

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace InfinityCode.OnlineMapsDemos
{
    [AddComponentMenu("Infinity Code/Online Maps/Demos/Mapbox Search Panel")]
    public class MapboxSearchPanel : MonoBehaviour
    {
        public InputField inputField;
        private OnlineMapsMarker marker;

        public void Search()
        {
            if (!OnlineMapsKeyManager.hasMapbox)
            {
                Debug.LogWarning("Please enter Map / Key Manager / Mapbox");
                return;
            }

            if (inputField == null) return;
            if (inputField.text.Length < 3) return;

            string locationName = OnlineMapsWWW.EscapeURL(inputField.text);
            string key = OnlineMapsKeyManager.Mapbox();

            OnlineMapsWWW www = new OnlineMapsWWW($"https://api.mapbox.com/geocoding/v5/mapbox.places/{locationName}.json?access_token={key}");
            www.OnComplete += OnGeocodingComplete;
        }

        private void OnGeocodingComplete(OnlineMapsWWW www)
        {
            if (www.hasError)
            {
                Debug.Log(www.error);
                return;
            }

            OnlineMapsJSONItem json = OnlineMapsJSON.Parse(www.text);

            OnlineMapsJSONItem feature = json["features/0"];
            OnlineMapsJSONItem centerJSON = feature["center"];
            Vector2 centerPoint = new Vector2(centerJSON[0].V<float>(), centerJSON[1].V<float>());

            if (marker == null) marker = OnlineMapsMarkerManager.CreateItem(centerPoint);
            else
            {
                marker.position = centerPoint;
            }

            marker.label = feature["place_name"].V<string>();

            OnlineMapsJSONItem bbox = feature["bbox"];
            Vector2 topLeft = new Vector2(bbox[0].V<float>(), bbox[1].V<float>());
            Vector2 bottomRight = new Vector2(bbox[2].V<float>(), bbox[3].V<float>());

            Vector2 center;
            int zoom;
            OnlineMapsUtils.GetCenterPointAndZoom(new Vector2[] { topLeft, bottomRight }, out center, out zoom);
            OnlineMaps.instance.SetPositionAndZoom(centerPoint.x, centerPoint.y, zoom);
        }

        private void Update()
        {
            EventSystem eventSystem = EventSystem.current;
            if ((Input.GetKeyUp(KeyCode.KeypadEnter) || Input.GetKeyUp(KeyCode.Return)) && eventSystem.currentSelectedGameObject == inputField.gameObject)
            {
                Search();
            }
        }
    }
}
Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: MapBox Search

Thank you Alex.
Works great only one time smile
It throws an error at me when I try to search for second location

NullReferenceException: Object reference not set to an instance of an object
InfinityCode.OnlineMapsDemos.MapboxSearchPanel.OnGeocodingComplete (OnlineMapsWWW www) (at Assets/Scripts/MapboxSearchPanel.cs:49)

Re: MapBox Search

I updated the script in post 2.

Kind Regards,
Infinity Code Team.

Boost your productivity a lot and immediately using Ultimate Editor Enhancer. Trial and non-commerce versions available.

Re: MapBox Search

Thank you Alex