convertviewportpointgetocoordinate在WP8.0中给出null

本文关键字:null WP8 convertviewportpointgetocoordinate | 更新日期: 2023-09-27 18:11:18

我试图获得地图控件左上角和右下角的坐标。我使用convertviewportpointtotocoordinate来做到这一点,但出于某种原因,它在使用WP 8.0时不会产生值,但在使用WP 8.1时它可以工作。你知道为什么会这样吗?

XAML:

<Controls:Map x:Name="RMap2" Width="400" Height="400" ZoomLevel="7" Loaded="RMap2_Loaded" TransformCenter="0,0" />
c#:

GeoCoordinate topLeft = RMap2.ConvertViewportPointToGeoCoordinate(new Point(0, 0));
GeoCoordinate bottomRight = RMap2.ConvertViewportPointToGeoCoordinate(new Point(400, 400));

convertviewportpointgetocoordinate在WP8.0中给出null

您需要在转换之前将音高设置为缩放级别。

GeoCoordinate topLeft = new GeoCoordinate();
GeoCoordinate bottomRight = new GeoCoordinate();
try
{
    //Set pitch to zoom level so ConvertViewpointToGeoCoordinate does not return null in 8.0
    RMap2.Pitch = RMap2.ZoomLevel;
    topLeft = RMap2.ConvertViewportPointToGeoCoordinate(new Point(0, 0));
    bottomRight = RMap2.ConvertViewportPointToGeoCoordinate(new Point(400, 400));
}
catch {  }
//Resets pitch to 0.0 so the map looks 2D
//These lines of code execute so quickly the user will not see the pitch change
RMap2.Pitch = 0.0;

值得注意的是,如果您的映射不可见,则在转换期间将得到null。如果可能的话,你可以考虑使用半径。下面是一个例子:

//Declare radius variable
double radius;
try
{
    //Set pitch to zoom level so ConvertViewpointToGeoCoordinate does not return null in 8.0
    RMap2.Pitch = RMap2.ZoomLevel;
    //Gets the distance between the center and the center left edge in meters
    radius = RMap2.Center.GetDistanceTo(radarMap.ConvertViewportPointToGeoCoordinate(new Point(0, 200)));
    //Converts meters to nautical miles
    radius = radius * 0.00053996;
}
//If your map is not visible, ConvertViewpointToGeoCoordinate will return null, and you better have a good backup plan
//Since this usually only happens when the app is first loading, I have determined the distance for when zoom level is 7.0 (using the code above, I just set the zoom level to 7.0, and then set a breakpoint on the last line to check the radius)
//You could do other things here like change it to visible
catch { RMap2.ZoomLevel = 7.0; radius = 98.766450549077661; }
//Resets pitch to 0.0 so the map looks 2D
//These lines of code execute so quickly the user will not see the pitch change
RMap2.Pitch = 0.0;