buğra coşkun
buğra coşkun

Reputation: 63

How to get destination point?

I am trying to navigation application on Mapbox with Unity. I want to get destination point from the user. I mean that user will write to where it wants to go and i will plot the road on the map according to destination point.

I tried reload map scrip, but it is just geolocate the map.

    namespace Mapbox.Examples
{
    using Mapbox.Geocoding;
    using UnityEngine.UI;
    using Mapbox.Unity.Map;
    using UnityEngine;
    using System;
    using System.Collections;

    public class ReloadMap : MonoBehaviour
    {
        Camera _camera;
        Vector3 _cameraStartPos;
        AbstractMap _map;

        [SerializeField]
        ForwardGeocodeUserInput _forwardGeocoder;

        [SerializeField]
        Slider _zoomSlider;

        private HeroBuildingSelectionUserInput[] _heroBuildingSelectionUserInput;

        Coroutine _reloadRoutine;

        WaitForSeconds _wait;

        void Awake()
        {
            _camera = Camera.main;
            _cameraStartPos = _camera.transform.position;
            _map = FindObjectOfType<AbstractMap>();
            if(_map == null)
            {
                Debug.LogError("Error: No Abstract Map component found in scene.");
                return;
            }
            if (_zoomSlider != null)
            {
                _map.OnUpdated += () => { _zoomSlider.value = _map.Zoom; };
                _zoomSlider.onValueChanged.AddListener(Reload);
            }
            if(_forwardGeocoder != null)
            {
                _forwardGeocoder.OnGeocoderResponse += ForwardGeocoder_OnGeocoderResponse;
            }
            _heroBuildingSelectionUserInput = GetComponentsInChildren<HeroBuildingSelectionUserInput>();
            if(_heroBuildingSelectionUserInput != null)
            {
                for (int i = 0; i < _heroBuildingSelectionUserInput.Length; i++)
                {
                    _heroBuildingSelectionUserInput[i].OnGeocoderResponse += ForwardGeocoder_OnGeocoderResponse;
                }
            }
            _wait = new WaitForSeconds(.3f);
        }

        void ForwardGeocoder_OnGeocoderResponse(ForwardGeocodeResponse response)
        {
            if (null != response.Features && response.Features.Count > 0)
            {
                int zoom = _map.AbsoluteZoom;
                _map.UpdateMap(response.Features[0].Center, zoom);
            }
        }

        void ForwardGeocoder_OnGeocoderResponse(ForwardGeocodeResponse response, bool resetCamera)
        {
            if (response == null)
            {
                return;
            }
            if (resetCamera)
            {
                _camera.transform.position = _cameraStartPos;
            }
            ForwardGeocoder_OnGeocoderResponse(response);
        }

        void Reload(float value)
        {
            if (_reloadRoutine != null)
            {
                StopCoroutine(_reloadRoutine);
                _reloadRoutine = null;
            }
            _reloadRoutine = StartCoroutine(ReloadAfterDelay((int)value));
        }

        IEnumerator ReloadAfterDelay(int zoom)
        {
            yield return _wait;
            _camera.transform.position = _cameraStartPos;
            _map.UpdateMap(_map.CenterLatitudeLongitude, zoom);
            _reloadRoutine = null;
        }
    }
}

In the direction example, it is just return geojson file. I could not able to plot the route on the map according to writing location. How can i define the destination point on the map by text.

Upvotes: 0

Views: 365

Answers (1)

Jordy Webb
Jordy Webb

Reputation: 84

For this you can use the directions API inside of the Unity SDK. Check out the traffic and directions example. You'll see how the response is being drawn as a line and rendered on a map. The DirectionsFactory.cs script draws a line along the route with the assigned material.

Upvotes: 3

Related Questions