如何获得目的地? [英] How to get destination point?

查看:92
本文介绍了如何获得目的地?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

推荐答案

为此,您可以使用Unity SDK中的Directions API.查看交通和路线示例.您将看到如何将响应绘制为一条线并呈现在地图上. DirectionsFactory.cs脚本沿着路线用指定的材质绘制一条线.

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.

这篇关于如何获得目的地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆