ConvertViewportPointToGeoCoordinate在WP8.0中给出null

时间:2014-07-03 05:57:55

标签: c# windows-phone-8

我正在尝试获取Map控件的左上角和右下角的坐标。我正在使用ConvertViewportPointToGeoCoordinate执行此操作但由于某些原因,使用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));

1 个答案:

答案 0 :(得分:1)

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

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;

希望这有帮助。