52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
using System;
|
||
using System.Collections;
|
||
using UnityEngine;
|
||
using Coolape;
|
||
|
||
public static class LBSUtl
|
||
{
|
||
//将tile(瓦片)坐标系转换为LatLngt(地理)坐标系,pixelX,pixelY为图片偏移像素坐标
|
||
public static LatLng TileXYToLatLng(int tileX, int tileY, int zoom, int pixelX = 0, int pixelY = 0)
|
||
{
|
||
double size = Math.Pow(2, zoom);
|
||
double pixelXToTileAddition = pixelX / 256.0;
|
||
double lng = (tileX + pixelXToTileAddition) / size * 360.0 - 180.0;
|
||
|
||
double pixelYToTileAddition = pixelY / 256.0;
|
||
double lat = Math.Atan(Math.Sinh(Math.PI * (1 - 2 * (tileY + pixelYToTileAddition) / size))) * 180.0 / Math.PI;
|
||
return new LatLng(lng, lat);
|
||
}
|
||
|
||
//将LatLngt地理坐标系转换为tile瓦片坐标系,pixelX,pixelY为图片偏移像素坐标
|
||
public static void LatLngToTileXY(LatLng latlng, int zoom, out int tileX, out int tileY, out int pixelX, out int pixelY)
|
||
{
|
||
double size = Math.Pow(2, zoom);
|
||
double x = ((latlng.Longitude + 180) / 360) * size;
|
||
double lat_rad = latlng.Latitude * Math.PI / 180;
|
||
double y = (1 - Math.Log(Math.Tan(lat_rad) + 1 / Math.Cos(lat_rad)) / Math.PI) / 2;
|
||
y = y * size;
|
||
|
||
tileX = (int)x;
|
||
tileY = (int)y;
|
||
pixelX = (int)((x - tileX) * 256);
|
||
pixelY = (int)((y - tileY) * 256);
|
||
}
|
||
|
||
}
|
||
|
||
|
||
public class LatLng
|
||
{
|
||
public double Longitude = 0;
|
||
public double Latitude = 0;
|
||
public LatLng(double Longitude, double Latitude)
|
||
{
|
||
this.Longitude = Longitude;
|
||
this.Latitude = Latitude;
|
||
}
|
||
public override string ToString()
|
||
{
|
||
return "Longitude:" + Longitude + ", Latitude:" + Latitude;
|
||
}
|
||
}
|