【发布时间】:2014-08-22 11:45:23
【问题描述】:
我试图在 WinRT Universal APP 的 bing 地图上拟合 2 个 pois,通常是用户位置和任何其他点,以便用户可以看到两点之间的地图,我有这个代码:
private void centerMap(Geoposition pos, MapPoi pos2)
{
try {
#if WINDOWS_APP
//TODO PC Maps
#elif WINDOWS_PHONE_APP
GeoboundingBox geoboundingBox = new Windows.Devices.Geolocation.GeoboundingBox(
new BasicGeoposition() { Latitude = pos.Coordinate.Latitude, Longitude = pos.Coordinate.Longitude },
new BasicGeoposition() { Latitude = pos2.lat, Longitude = pos2.lng });
map1._map.TrySetViewBoundsAsync(geoboundingBox,null,MapAnimationKind.Linear);
#endif
}catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
所以我又遇到了一个我自己无法解决的问题。
首先我需要了解一些我必须做的坏事,我在 GeoboundingBox 构造函数上得到 System.ArgumentException,这里是堆栈跟踪:
A first chance exception of type 'System.ArgumentException' occurred in EspartAPP.WindowsPhone.exe
System.ArgumentException: Value does not fall within the expected range.
at Windows.Devices.Geolocation.GeoboundingBox..ctor(BasicGeoposition northwestCorner, BasicGeoposition southeastCorner)
at EspartAPP.Modulos.Modulo_Mapa.centerMap(Geoposition pos, MapPoi pos2)
这里是用于测试的坐标:
pos var: Lat 40.4564664510315 lng -3.65939843190291
pos2 var: Lat 40.4579103 lngs -3.6532357
两个坐标都是正确的,一个是刚从gps得到的用户位置,另一个是一个很近的位置。
我不明白什么会变坏,一定有什么我需要知道的。
我发布的代码只是抛出了那个异常,但是,如果我只是对 pos 纬度做 +1,它就可以工作,而且似乎做得对。
如果我首先添加 pos2(作为northwestCorner),它不会抛出异常,它会缩小到最大,所以我几乎可以看到整个地图(这显然是错误的)
一定有一些规则我忽略了,也许我必须计算哪个坐标应该在西北位置?
编辑:正是这样,工作代码:
private void centerMap(Geoposition pos, MapPoi pos2)
{
try {
BasicGeoposition nw = new BasicGeoposition();
nw.Latitude = Math.Max(pos.Coordinate.Latitude, pos2.lat);
nw.Longitude = Math.Min(pos.Coordinate.Longitude, pos2.lng);
BasicGeoposition se = new BasicGeoposition();
se.Latitude = Math.Min(pos.Coordinate.Latitude, pos2.lat);
se.Longitude = Math.Max(pos.Coordinate.Longitude, pos2.lng);
#if WINDOWS_APP
//TODO PC Maps
#elif WINDOWS_PHONE_APP
GeoboundingBox geoboundingBox = new Windows.Devices.Geolocation.GeoboundingBox(nw,se);
map1._map.TrySetViewBoundsAsync(geoboundingBox,null,MapAnimationKind.Bow);
#endif
}catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
抱歉,我的英语不好和这个 bing 地图的问题,我希望这真的很差,因为它的 API 较新,而且很难找到你正在使用的 API 所需的东西。
提前致谢。
【问题讨论】:
标签: c# windows-runtime windows-phone bing-maps