如何获得目的地?

时间:2019-03-05 13:41:41

标签: c# unity3d mapbox

我正在尝试使用Unity在Mapbox上导航应用程序。我想从用户那里获得目的地。我的意思是用户将写信到它想去的地方,然后我将根据目的地在地图上绘制道路。

我尝试重新加载地图脚本,但这只是对地图进行地理位置定位。

    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;
        }
    }
}

在方向示例中,它只是返回geojson文件。我无法根据书写位置在地图上绘制路线。如何通过文本在地图上定义目的地。

1 个答案:

答案 0 :(得分:2)

为此,您可以使用Unity SDK中的Directions API。签出traffic and directions example。您将看到如何将响应绘制为线条并呈现在地图上。 DirectionsFactory.cs脚本沿着路线与分配的材料划了一条线。