Ejemplo n.º 1
0
        /// <summary>
        /// 移动地图
        /// </summary>
        /// <param name="offset">屏幕像素单位</param>
        public void MoveMap(Vector2 offset)
        {
            //相机位置
            Vector3 vector      = new Vector3(offset.x, 0, offset.y);
            Vector3 worldOffset = transform.TransformPoint(vector * MapFunction.GetCameraMoveScale(mMapZoomLevel));

            mMapCamera.transform.position -= worldOffset;
            //判断移动是否超出范围
            Vector3 nextCameraPos    = mMapCamera.transform.position;
            Vector3 leftDownWorldPos = ScreenToWorldPoint(new Vector3(0, 0));
            Vector3 rightUpWorldPos  = ScreenToWorldPoint(new Vector3(Screen.width, Screen.height));

            if (leftDownWorldPos.x < mWorldPosRange.x)
            {
                nextCameraPos.x += mWorldPosRange.x - leftDownWorldPos.x;
            }
            else if (mWorldPosRange.width < rightUpWorldPos.x)
            {
                nextCameraPos.x += mWorldPosRange.width - rightUpWorldPos.x;
            }
            if (leftDownWorldPos.z < mWorldPosRange.y)
            {
                nextCameraPos.z += mWorldPosRange.y - leftDownWorldPos.z;
            }
            else if (mWorldPosRange.height < rightUpWorldPos.z)
            {
                nextCameraPos.z += mWorldPosRange.height - rightUpWorldPos.z;
            }
            mMapCamera.transform.position = nextCameraPos;
            //计算中心经纬度
            mCenterLngLat = WorldPos2LngLat(mMapCamera.transform.position);
        }
Ejemplo n.º 2
0
        public Vector3 ScreenToWorldPoint(Vector2 screenPos)
        {
            double    screenX = screenPos.x;
            double    screenY = screenPos.y;
            double    screenZ = MapFunction.GetCameraHeight(mMapZoomLevel);
            Matrix4x4 pMatrix = mMapCamera.projectionMatrix;

            //反齐次除法,求出裁剪空间坐标
            double px = screenX / Screen.width;

            px = (px - 0.5f) * 2f;
            double py = screenY / Screen.height;

            py = (py - 0.5f) * 2f;
            double pz = (-screenZ - pMatrix.m23) / pMatrix.m22;
            double pw = screenZ;

            px *= pw;
            py *= pw;
            //裁剪空间到相机空间
            Matrix4x4 pInverseMatrix = mMapCamera.projectionMatrix.inverse;
            double    vx             = (pInverseMatrix.m00 * px + pInverseMatrix.m01 * py + pInverseMatrix.m02 * pz + pInverseMatrix.m03 * pw);
            double    vy             = (pInverseMatrix.m10 * px + pInverseMatrix.m11 * py + pInverseMatrix.m12 * pz + pInverseMatrix.m13 * pw);
            double    vz             = (pInverseMatrix.m20 * px + pInverseMatrix.m21 * py + pInverseMatrix.m22 * pz + pInverseMatrix.m23 * pw);
            //观察空间到世界空间
            Matrix4x4 vInverseMatrix = mMapCamera.worldToCameraMatrix.inverse;
            double    x = (vInverseMatrix.m00 * vx + vInverseMatrix.m01 * vy + vInverseMatrix.m02 * vz + vInverseMatrix.m03 * 1);
            double    y = (vInverseMatrix.m10 * vx + vInverseMatrix.m11 * vy + vInverseMatrix.m12 * vz + vInverseMatrix.m13 * 1);
            double    z = (vInverseMatrix.m20 * vx + vInverseMatrix.m21 * vy + vInverseMatrix.m22 * vz + vInverseMatrix.m23 * 1);

            return(new Vector3((float)(x), (float)(y), (float)(z)));
        }
Ejemplo n.º 3
0
        public double Distance(TileData tileData)
        {
            Vector2D vector1 = MapFunction.Tile2Pixel(this);
            Vector2D vector2 = MapFunction.Tile2Pixel(tileData);

            return(vector1.Distance(vector2));
        }
Ejemplo n.º 4
0
        public TileData WorldPos2TileData(Vector3 worldPos, int zoom = -1)
        {
            if (-1 == zoom)
            {
                zoom = (int)mMapZoomLevel;
            }
            Vector2D lngLat = WorldPos2LngLat(worldPos);

            return(MapFunction.LngLat2Tile(lngLat, zoom));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 渲染地图
        /// </summary>
        public void DoRender()
        {
            //相机坐标
            Vector3 centerPos = LngLat2WorldPos(mCenterLngLat);
            //相机高度
            Vector3 heightVec = new Vector3(0, (float)MapFunction.GetCameraHeight(mMapZoomLevel), 0);
            Vector3 cameraPos = centerPos + heightVec;

            mMapCamera.transform.position = cameraPos;
            //计算相机范围
            Vector3         leftDownWorldPos = ScreenToWorldPoint(new Vector3(-0.1f * Screen.width, -0.1f * Screen.height, heightVec.y));
            Vector3         rightUpWorldPos  = ScreenToWorldPoint(new Vector3(1.1f * Screen.width, 1.1f * Screen.height, heightVec.y));
            TileData        leftDownTile     = WorldPos2TileData(leftDownWorldPos);
            TileData        rightUpTile      = WorldPos2TileData(rightUpWorldPos);
            List <TileData> allTileDatas     = new List <TileData>();

            for (int i = leftDownTile.tile.x; i <= rightUpTile.tile.x; i++)
            {
                for (int k = leftDownTile.tile.y; k <= rightUpTile.tile.y; k++)
                {
                    allTileDatas.Add(new TileData((int)mMapZoomLevel, i, k));
                }
            }
            //中心Tile
            TileData centerTileData = WorldPos2TileData(centerPos);

            //按照距离排序,距离近的优先加载
            allTileDatas.Sort((tileData1, tileData2) => {
                double distance1 = centerTileData.Distance(tileData1);
                double distance2 = centerTileData.Distance(tileData2);
                if (distance1.DoubleIsEqual(distance2))
                {
                    return(0);
                }
                return(distance1 < distance2 ? -1 : 1);
            });
            for (int i = 0; i < allTileDatas.Count; i++)
            {
                MapTile mapTile = GetMapTile(allTileDatas[i]);
                mapTile.transform.position = TileData2WorldPos(allTileDatas[i]);
            }
            //清理距离远的Tile
            if (0 < allTileDatas.Count)
            {
                TileData maxDisTile  = allTileDatas[allTileDatas.Count - 1];
                double   maxDistance = maxDisTile.Distance(centerTileData);
                ClearMapTile(centerTileData, maxDistance);
            }
        }
Ejemplo n.º 6
0
 public float GetTileScale()
 {
     return(MapFunction.GetTileScale((int)mMapZoomLevel));
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 当前地图的缩放比例
 /// </summary>
 /// <returns></returns>
 public float GetMapScale()
 {
     return(MapFunction.GetCameraMoveScale(mMapZoomLevel));
 }
Ejemplo n.º 8
0
        public Vector3 TileData2WorldPos(TileData tileData)
        {
            Vector2D lngLat = MapFunction.Tile2LngLat(tileData);

            return(LngLat2WorldPos(lngLat));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 世界坐标转经纬度
        /// </summary>
        /// <param name="worldPos"></param>
        /// <returns></returns>
        public Vector2D WorldPos2LngLat(Vector3 worldPos)
        {
            Vector3 position = mTileParent.InverseTransformPoint(worldPos);

            return(MapFunction.Position2LngLat(position));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 经纬度转世界坐标
        /// </summary>
        /// <param name="lngLat"></param>
        /// <returns></returns>
        public Vector3 LngLat2WorldPos(Vector2D lngLat)
        {
            Vector3 position = MapFunction.LngLat2Position(lngLat);

            return(mTileParent.TransformPoint(position));
        }
Ejemplo n.º 11
0
 /// <summary>
 /// 获取Tile在场景中图片的缩放比例
 /// </summary>
 /// <returns></returns>
 public float GetTileScale()
 {
     return(MapFunction.GetTileScale(zoom));
 }