C# 根据墨卡托投影上的纬度(以度为单位)计算Y位置

C# 根据墨卡托投影上的纬度(以度为单位)计算Y位置,c#,.net,mapping,latitude-longitude,C#,.net,Mapping,Latitude Longitude,我试图用墨卡托投影计算地图上的Y位置,给定纬度单位为度。我需要的是: //mapHeight might be 600 (pixels), for example //latitudeInDegrees ranges from -90 to 90 public double CalculateY(double mapHeight, double latitudeInDegrees) { //what on earth do I do here to calculate the Y off

我试图用墨卡托投影计算地图上的Y位置,给定纬度单位为度。我需要的是:

//mapHeight might be 600 (pixels), for example
//latitudeInDegrees ranges from -90 to 90
public double CalculateY(double mapHeight, double latitudeInDegrees)
{
    //what on earth do I do here to calculate the Y offset on the map?
    return ???;
}
我尝试过在网上找到的各种东西(包括wikipedia和stackoverflow),但没有一种对我有用。我可能在做傻事,但我不知道是什么。谁能拯救我的理智

public double CalculateY(double mapHeight, double latitudeInDegrees)
{
    return Math.Log(Math.Tan(latitudeInDegrees / 360d * Math.PI + Math.PI / 4));
}
不记得使用mapHeight缩放y(您应该知道taht的latitudeInDegrees的最大值和最小值)

引自维基百科:

纬度高于北纬70°或 南部,墨卡托投影为 几乎无法使用

您应该编写如下代码:

private double CalculateYRelative(double latitudeInDegrees)
{
    return Math.Log(Math.Tan(latitudeInDegrees / 360d * Math.PI + Math.PI / 4));
}
...
minY = CalculateYRelative(MinLatitudeInDegrees);
maxY = CalculateYRelative(MaxLatitudeInDegrees);
...
public double CalculateY(double mapHeight, double latitudeInDegrees)
{
    return mapHeight*
           (CalculateYRelative(latitudeInDegrees) - minY) / (maxY - minY);
}
有关更多信息:


很抱歉编辑了很多内容。我创建了我答案的发行版。你关于缩放的评论对我来说毫无意义。你能澄清一下吗?一旦我知道了最大和最小纬度,我该如何根据地图高度来调整?非常感谢。我已经看到了所有与你相关的答案,但没有一个能充分解释这一点。您的编辑帮助我理解了这一点。@spanks,请参阅我的新编辑。MinLatitudeInDegrees和MaxLatitudeInDegrees是您的常量。您应该使用此常量计算minY和maxY,并在CalculateY中使用结果。CalculateY返回[0,maxHeight]中的值