【发布时间】:2014-11-15 16:40:05
【问题描述】:
我想在我的地图中添加一个“垫片”,这样极端图钉就不会一半在界内,一半在界外。我在正确的轨道上(没有双关语),但我的逻辑是错误的。我写了这个方法:
// Adapted from Brundritt and Boonaert: https://stackoverflow.com/questions/26937358/can-i-adjust-my-bing-maps-view-locationrect-bounding-box-by-a-small-amount
public static Location GetAShimLocation(IList<Location> locations, bool IsForNorthwestCorner)
{
const double MAP_CUSHION = 0.1; // Is this a comfortable enough cushion?
// I don't know why the Lats are 85 instead of 90
double maxLat = -85;
double minLat = 85;
double maxLon = -180;
double minLon = 180;
foreach (Location loc in locations)
{
if (loc.Latitude > maxLat)
{
maxLat = loc.Latitude;
}
if (loc.Latitude < minLat)
{
minLat = loc.Latitude;
}
if (loc.Longitude > maxLon)
{
maxLon = loc.Longitude;
}
if (loc.Longitude < minLon)
{
minLon = loc.Longitude;
}
}
Location retLoc = new Location();
// I'm not sure this math is right - test it later
if (IsForNorthwestCorner)
{
retLoc.Latitude = maxLat - MAP_CUSHION;
retLoc.Longitude = maxLon - MAP_CUSHION;
}
else // SouthEast corner - stretch a little both directions
{
retLoc.Latitude = minLat + MAP_CUSHION;
retLoc.Longitude = minLon + MAP_CUSHION;
}
return retLoc;
}
...然后这样称呼它:
private void ResizeMap()
{
App.photosetLocationCollection.Add(
PhotraxUtils.GetAShimLocation(App.photosetLocationCollection, true));
App.photosetLocationCollection.Add(
PhotraxUtils.GetAShimLocation(App.photosetLocationCollection, false));
photraxMap.SetView(new LocationRect(App.photosetLocationCollection));
}
...并且它确实改变了地图的大小,而不是将“垫片”位置添加为图钉,而是将地图向上拉一点向北和向西,向南和向东,它似乎在垂直挤压它(缩小纬度范围)并水平拉伸它(增加经度)。
这里我真的需要什么最小值和最大值以及减号和加号的组合?
总结一下:我正在寻找的是将地图从西北角向西北“拉伸”一点,从东南角向东南再一点。
50 点(或更多)事后奖励,奖励给发现这一点的人。
更新
这是在合并 Jan Kukacka 的代码之后,并在垫子上玩耍,直到我找到了正确的地方; YMMV(没有双关语)。
public static Location GetAShimLocation(IList<Location> locations, bool IsForNorthwestCorner)
{
const double MAP_CUSHION = 1.05; // This seems to be about perfect
//double maxLat = -85; <= This is what the original (ad[a,o]pted) code was, I don't know why 85 instead of 90, though
//double minLat = 85;
double maxLat = -90;
double minLat = 90;
double maxLon = -180;
double minLon = 180;
foreach (Location loc in locations)
{
if (loc.Latitude > maxLat)
{
maxLat = loc.Latitude;
}
if (loc.Latitude < minLat)
{
minLat = loc.Latitude;
}
if (loc.Longitude > maxLon)
{
maxLon = loc.Longitude;
}
if (loc.Longitude < minLon)
{
minLon = loc.Longitude;
}
}
Location retLoc = new Location();
double latDif = Math.Abs(maxLat - minLat);
double lonDif = Math.Abs(maxLon - minLon);
// This logic from Jan Kukacka https://stackoverflow.com/questions/26948104/which-longitude-latitude-adjustments-do-i-need-to-enlarge-my-map-to-the-northwes
if (IsForNorthwestCorner)
{
retLoc.Latitude = maxLat - MAP_CUSHION * latDif;
retLoc.Longitude = maxLon - MAP_CUSHION * lonDif;
}
else // SouthEast corner - stretch a little both directions
{
retLoc.Latitude = minLat + MAP_CUSHION * latDif;
retLoc.Longitude = minLon + MAP_CUSHION * lonDif;
}
// Edge case insurance
if (retLoc.Latitude > 90.0)
{
retLoc.Latitude = 90.0;
}
else if (retLoc.Latitude < -90.0)
{
retLoc.Latitude = -90.0;
}
if (retLoc.Longitude > 180.0)
{
retLoc.Latitude = 180.0;
}
else if (retLoc.Longitude < -180.0)
{
retLoc.Longitude = -180.0;
}
return retLoc;
}
【问题讨论】:
-
:赏金在哪里?你能详细说明一下吗?
-
你只能在问题搁置了几天后才能设置赏金,所以我还不能设置。但事后,我会(“奖励现有答案”)。所以,如果有一个可行的答案,我会在两天内奖励这个问题,然后在一天后奖励赏金(奖励赏金有一个“冷静期”)。我希望那里有一些熟练的赏金猎人!
-
为了清楚起见,您希望将图钉移动到不正确的位置,以便它们在视野范围内——显然您无法将地图扩展到 [-180, 90] 之外, [180, -90]。我知道这不是把猫送上月球,正如你所说的那样,所以你正在寻找一种依赖比例的方法来移动针脚,使其完全在视野中?
-
我不想要伪造的图钉(然后用户会点击它们,然后对我感到失望和/或生气),而是要伪造的位置,一个在西北,一个在东南。是的,这样图钉是完全可见的,而不是部分可见的。
标签: c# geolocation geospatial latitude-longitude bing-maps