如何动态设置GeoCoordinate lat + lng值

时间:2011-08-01 01:09:31

标签: windows-phone-7 bing-maps

我正在尝试动态设置标记为"<device:GeoCoordinate..."的项目的lat / lng值。我可以在XAML中对此进行硬编码没有问题,但是当我尝试设置它时,我得到一个空指针异常(在下面显示的加载处理程序内)

XAML

<maps:Map ZoomLevel="6" Mode="Aerial" Grid.Row="1" Grid.ColumnSpan="2">
        <maps:Map.Center>
            <device:GeoCoordinate x:Name="theLocation"/>
        </maps:Map.Center>
        <maps:MapLayer x:Name="TheMapLayer"/>
    </maps:Map>

C#

    void ViewBingMaps_Loaded(object sender, RoutedEventArgs e)
    {
        //get lat + lng for the device and set it dynamically
        theLocation.Latitude = 41.5686949;
        theLocation.Longitude = -93.7943157;
    }

是否可以设置此设置或我是否误解设备:GeoCoordinate目的?

2 个答案:

答案 0 :(得分:1)

只需在代码隐藏中重新实例化GeoCoordinate类并将地图中心设置为它:

theLocation = new System.Device.Location.GeoCoordinate(10.0, 10.0);
map1.Center = theLocation;

答案 1 :(得分:0)

或者,使用MVVM方法:

<maps:Map Center="{Binding Center}">
    ...
</maps:Map>

然后绑定到名为GeoCoordinate的{​​{1}}属性。

Center

然后可以从ViewModel动态更改它(你执行有一个ViewModel,对吧?!)

同样,你可以绑定ZoomProperty(这往往相当方便),几乎所有其他东西!