2021-04-07 17:22:45 +08:00
|
|
|
|
using Mapbox.Unity.Map;
|
|
|
|
|
|
using Mapbox.Unity.MeshGeneration.Data;
|
|
|
|
|
|
using Mapbox.Utils;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Assets.Scenes.Ride.Scripts
|
|
|
|
|
|
{
|
|
|
|
|
|
public class InitMiniMapProvider : MonoBehaviour
|
|
|
|
|
|
{
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
|
AbstractMap _map;
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
|
Camera _minicamera;
|
|
|
|
|
|
[SerializeField]
|
|
|
|
|
|
GameObject _player;
|
|
|
|
|
|
CyclingController cyclingCotroller;
|
|
|
|
|
|
PlayerController playerController;
|
|
|
|
|
|
GameObject _mipMapRoute;
|
|
|
|
|
|
void Start()
|
|
|
|
|
|
{
|
|
|
|
|
|
cyclingCotroller = FindObjectOfType<CyclingController>();
|
|
|
|
|
|
playerController = FindObjectOfType<PlayerController>();
|
|
|
|
|
|
if (cyclingCotroller != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var mapdata = cyclingCotroller.GetMapData();
|
|
|
|
|
|
//初始化map
|
2021-04-19 14:38:31 +08:00
|
|
|
|
//var point = cyclingCotroller.GetCenterCoordinate();
|
2021-04-12 17:35:56 +08:00
|
|
|
|
if (_map != null && mapdata !=null)
|
2021-04-07 17:22:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
_map.OnInitialized += _map_OnInitialized;
|
|
|
|
|
|
_map.OnUpdated += _map_OnUpdated;
|
|
|
|
|
|
_map.Initialize(new Vector2d(mapdata.Center[0], mapdata.Center[1]), 12);
|
|
|
|
|
|
var bbox = mapdata.Bbox;
|
|
|
|
|
|
var targetbounds = new Vector2dBounds(new Vector2d(bbox[0], bbox[1]), new Vector2d(bbox[2], bbox[3]));
|
|
|
|
|
|
var screenBounds = GetScreenBounds();
|
|
|
|
|
|
SetZoomToFitBounds(targetbounds, screenBounds);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private void Update()
|
|
|
|
|
|
{
|
2021-04-12 17:35:56 +08:00
|
|
|
|
if (playerController != null)
|
|
|
|
|
|
{
|
2021-04-19 14:38:31 +08:00
|
|
|
|
_player.transform.localPosition = _map.GeoToWorldPosition(playerController.Currentlatlong);
|
2021-04-12 17:35:56 +08:00
|
|
|
|
}
|
2021-04-07 17:22:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
private bool init = false;
|
|
|
|
|
|
private void _map_OnInitialized()
|
|
|
|
|
|
{
|
|
|
|
|
|
init = true;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region Camera 自适应
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// https://github.com/mapbox/mapbox-unity-sdk/issues/1580
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="targetBounds">路线的边界</param>
|
|
|
|
|
|
/// <param name="screenBounds">小地图边界</param>
|
|
|
|
|
|
private void SetZoomToFitBounds(Vector2dBounds targetBounds, Vector2dBounds screenBounds)
|
|
|
|
|
|
{
|
|
|
|
|
|
var targetLonDelta = targetBounds.East - targetBounds.West;
|
|
|
|
|
|
var targetLatDelta = targetBounds.North - targetBounds.South;
|
|
|
|
|
|
|
|
|
|
|
|
var screenLonDelta = screenBounds.East - screenBounds.West;
|
|
|
|
|
|
var screenLatDelta = screenBounds.North - screenBounds.South;
|
|
|
|
|
|
|
|
|
|
|
|
var zoomLatMultiplier = screenLatDelta / targetLatDelta;
|
|
|
|
|
|
var zoomLonMultiplier = screenLonDelta / targetLonDelta;
|
|
|
|
|
|
|
|
|
|
|
|
var latZoom = Math.Log(zoomLatMultiplier, 2);
|
|
|
|
|
|
var lonZoom = Math.Log(zoomLonMultiplier, 2);
|
|
|
|
|
|
|
|
|
|
|
|
var zoom = (float)(_map.Zoom + Math.Min(latZoom, lonZoom));
|
|
|
|
|
|
|
|
|
|
|
|
_map.SetZoom((float)Math.Floor(zoom));
|
|
|
|
|
|
_map.UpdateMap();
|
|
|
|
|
|
}
|
|
|
|
|
|
private Vector2dBounds GetScreenBounds()
|
|
|
|
|
|
{
|
|
|
|
|
|
var screenWidth = UnityEngine.Screen.width;
|
|
|
|
|
|
var screenHeight = UnityEngine.Screen.height;
|
|
|
|
|
|
|
|
|
|
|
|
var sw_world = _minicamera.ViewportToWorldPoint(new Vector3(0, 0, 180));
|
|
|
|
|
|
var sw = _map.WorldToGeoPosition(sw_world);
|
|
|
|
|
|
|
2021-04-19 18:38:28 +08:00
|
|
|
|
var ne_world = _minicamera.ViewportToWorldPoint(new Vector3(1f, 1f, 180));
|
2021-04-07 17:22:45 +08:00
|
|
|
|
var ne = _map.WorldToGeoPosition(ne_world);
|
|
|
|
|
|
|
|
|
|
|
|
return new Vector2dBounds(new Vector2d(sw.x, sw.y), new Vector2d(ne.x, ne.y));
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 创建小地图路线
|
|
|
|
|
|
private void _map_OnUpdated()
|
|
|
|
|
|
{
|
|
|
|
|
|
CreateMiniRoute();
|
|
|
|
|
|
}
|
|
|
|
|
|
void CreateMiniRoute()
|
|
|
|
|
|
{
|
|
|
|
|
|
var meshData = new MeshData();
|
|
|
|
|
|
var dat = new List<Vector3>();
|
|
|
|
|
|
var mapData = cyclingCotroller.GetMapData();
|
|
|
|
|
|
if (mapData != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var count = mapData.List.Count;
|
|
|
|
|
|
var interval = Math.Max(Math.Ceiling(count / 500D),1f);
|
|
|
|
|
|
for (int i = 0; i < mapData.List.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(i% interval == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var point = mapData.List[i].Point;
|
|
|
|
|
|
Vector3 item = _map.GeoToWorldPosition(new Vector2d(point[0], point[1]));
|
|
|
|
|
|
item.y += 1f;
|
|
|
|
|
|
dat.Add(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
var feat = new VectorFeatureUnity();
|
|
|
|
|
|
feat.Points.Add(dat);
|
|
|
|
|
|
CreateLineRender(feat);//创建小地图路线
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
//创建小地图路线
|
|
|
|
|
|
void CreateLineRender(VectorFeatureUnity feat)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_mipMapRoute != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_mipMapRoute.Destroy();
|
|
|
|
|
|
}
|
|
|
|
|
|
_mipMapRoute = new GameObject("MiniMapRoute");
|
|
|
|
|
|
_mipMapRoute.transform.parent = transform;
|
|
|
|
|
|
var lineRender = _mipMapRoute.AddComponent<LineRenderer>();
|
|
|
|
|
|
lineRender.material = new Material(Shader.Find("Sprites/Default"));
|
|
|
|
|
|
var dat = feat.Points[0];
|
2021-04-15 10:13:01 +08:00
|
|
|
|
lineRender.endColor = Color.white;
|
|
|
|
|
|
lineRender.startColor = Color.white;
|
2021-04-07 17:22:45 +08:00
|
|
|
|
//设置宽度
|
2021-04-15 10:13:01 +08:00
|
|
|
|
lineRender.startWidth = 1f;
|
|
|
|
|
|
lineRender.endWidth = 1f;
|
2021-04-07 17:22:45 +08:00
|
|
|
|
lineRender.positionCount = dat.Count;
|
|
|
|
|
|
lineRender.SetPositions(feat.Points[0].ToArray());
|
2021-04-15 10:13:01 +08:00
|
|
|
|
lineRender.numCornerVertices = 0;
|
|
|
|
|
|
lineRender.loop = false;
|
2021-04-07 17:22:45 +08:00
|
|
|
|
_mipMapRoute.layer = 9;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|