Files
tianrunCRM/Assets/3rd/LBSBaidu/Scripts/LBSUtl.cs
2020-07-04 14:41:25 +08:00

52 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using UnityEngine;
using Coolape;
public static class LBSUtl
{
//将tile(瓦片)坐标系转换为LatLngt(地理)坐标系pixelXpixelY为图片偏移像素坐标
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瓦片坐标系pixelXpixelY为图片偏移像素坐标
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;
}
}