如何对目的地点进行地理编码?

时间:2019-03-04 17:48:37

标签: unity3d mapbox

我正在尝试使用Unity在Mapbox上导航应用程序。我可以获取用户的位置,但是无法从用户获取目的地。我想作为文字。如果我能够到达目的地,则可以轻松地绘制路线。

我尝试了重新加载地图,但这没有用。另外,我尝试了正向地址解析用户输入文件,但无法成功。

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

        [RequireComponent(typeof(InputField))]
        public class ForwardGeocodeUserInput : MonoBehaviour
        {
            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<> OnGeocoderResponse = delegate { };
            public event Action<ForwardGeocodeResponse> OnGeocoderResponse = delegate { };

            void Awake()
            {
                _inputField = GetComponent<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;
                    //_inputField.text = string.Format("{0},{1}", center.x, center.y);
                    _coordinate = res.Features[0].Center;
                }
                Response = res;
                OnGeocoderResponse(res);
            }
        }
}

你能帮我吗?

0 个答案:

没有答案