开发者

How to set the GeoCoordinate lat + lng values dynamically

开发者 https://www.devze.com 2023-03-24 09:02 出处:网络
I\'m trying to set the lat/lng values for the item marked \"<device:GeoCoordinate...\" dynamically.I can hard code this in the XAML no problem but when I attempt to set it on the fly I get a null p

I'm trying to set the lat/lng values for the item marked "<device:GeoCoordinate..." dynamically. I can hard code this in the XAML no problem but when I attempt to set it on the fly I get a null pointer exception (inside the loaded handler shown below)

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;
    }

Is it possible to set this or am I misunderstanding the d开发者_StackOverflowevice:GeoCoordinate purpose?


Simply re-instantiate the GeoCoordinate class in the code-behind and set the map center to it:

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


Or alternatively, use a MVVM approach:

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

Which then binds to a GeoCoordinate property named Center.

public GeoCoordinate Center
{
    get;
    private set;
}

It can then be dynamically changed from your ViewModel (You do have a ViewModel, right?!)

Likewise you can bind the ZoomProperty (which tends to be rather handy), and pretty much everything else!

0

精彩评论

暂无评论...
验证码 换一张
取 消