Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get location from bing map on Tap event in mvvm

i'm trying to get location, when I tap on bing map control. I know how to get it, but I want to make it in ViewModel. For Tap event I use interactions.

<map:Map CopyrightVisibility="Collapsed" LogoVisibility="Collapsed">
            <map:MapTileLayer>
                <map:MapTileLayer.TileSources>
                    <customMap:OpenStreetTile />
                </map:MapTileLayer.TileSources>
            </map:MapTileLayer>
            <map:MapLayer>
                <map:Pushpin Location="{Binding Location}" />
            </map:MapLayer>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Tap">
                    <cmd:EventToCommand Command="{Binding AddPushpinCommand}" PassEventArgsToCommand="True" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </map:Map>

I can get position from eventarguments with GetPosition function. But this function need UIElement parameter.I made it so:

var source = (e.OriginalSource as MapLayer).ParentMap;
        var point = e.GetPosition(source);
        Location = source.ViewportPointToLocation(point);

But Im not sure if it is good solution. Do you have any idea how to do it? Thanks

like image 784
Pupino Avatar asked Feb 01 '26 04:02

Pupino


1 Answers

Well, your approach certainly works and sometimes, good enough is good enough. ;) If you want to be 100% clean with your MVVM implementation, you should avoid references to the view (and thus the MapLayer class) however.

Joost van Schaik wrote a blog post that shows how to achieve this using behaviors (it's for Windows Phone 8, but I'm sure it applies to Windows Phone 7 as well): http://dotnetbyexample.blogspot.nl/2013/03/simple-reverse-geocoding-with-windows.html

like image 57
Daniel Sklenitzka Avatar answered Feb 02 '26 19:02

Daniel Sklenitzka