在Windows Phone 8应用程序中绑定地图中心和图钉

时间:2013-09-30 10:19:01

标签: windows-phone-8 mvvm bing-maps

我正在开发一个Windows Phone 8应用程序,我必须使用图钉来实现地图控件,该图钉将显示用户的当前位置,我将当前位置分配给我的图钉,现在我希望此图钉位于中心的地图。任何人都可以帮助我将图钉绑定到地图的中心。

<maps:Map x:Name="MyMap" Center="{Binding}" ZoomLevel="15">
        <toolkit:MapExtensions.Children>
            <toolkit:Pushpin x:Name="pushpin1" GeoCoordinate="{Binding}">
                <toolkit:Pushpin.Template>
                    <ControlTemplate TargetType="toolkit:Pushpin">
                        <StackPanel>
                            <ContentPresenter x:Name="content" Content="{TemplateBinding Content}" HorizontalAlignment="Left"></ContentPresenter>
                            <Path Data="M0,0L1,1L2,0L2,0L1,0L0,0Z"
                              Fill="#00AAFF"
                              Stretch="Fill"
                              Margin="-2,0"
                              Height="120"
                              Width="30"
                              Visibility="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Visibility, Mode=TwoWay}"
                              HorizontalAlignment="Left"
                              />

                        </StackPanel>
                    </ControlTemplate>
                </toolkit:Pushpin.Template>
            </toolkit:Pushpin>
        </toolkit:MapExtensions.Children>
    </maps:Map>

1 个答案:

答案 0 :(得分:2)

您想在Windows Phone 8 App中使用Bing地图吗?我在WP8应用程序中遇到旧的Bing Map控件有问题。我写了windows phone 8 old map vs new map。 WP 8的最佳解决方案是新的诺基亚地图控件。

在XAML中:

 xmlns:nokiamap="clr-namespace:Microsoft.Phone.Maps.Controls;assembly=Microsoft.Phone.Maps"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Maps.Toolkit;assembly=Microsoft.Phone.Controls.Toolkit"

<nokiamap:Map Name="NokiaMap">
 <toolkit:MapExtensions.Children>
    <toolkit:Pushpin x:Name="MyPushpin"/>
 </toolkit:MapExtensions.Children>
</nokiamap:Map>

// Init Pushpin

private Pushpin MyPushpin { get; set; }

ObservableCollection<DependencyObject> children = MapExtensions.GetChildren(NokiaMap);       
    var pin = children.FirstOrDefault(x => x.GetType() == typeof(Pushpin)) as Pushpin;
    MyPushpin = pin;

//启动geolocator

  public void StartGeolocator()
        {
              Geolocator geolocator = new Geolocator();
                geolocator.DesiredAccuracy = PositionAccuracy.High;
                geolocator.MovementThreshold = 10; 
                geolocator.PositionChanged += geolocator_PositionChanged;
                geolocator.StatusChanged += geolocator_StatusChanged;
            }
        }

 private void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
    {
       myGeoposition = new GeoCoordinate()
       {
            Latitude = args.Position.Coordinate.Latitude,
            Longitude = args.Position.Coordinate.Longitude,
       }
       MyPushpin.GeoCoordinate = myGeoposition;
       NokiaMap.SetView(myGeoposition, NokiaMap.ZoomLevel);
    }

查看您的代码Pushpin location binding in windows phone 8 app is not working

void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
    Dispatcher.BeginInvoke(() =>
    {
        ObservableCollection<DependencyObject> children = MapExtensions.GetChildren(MyMap);       
        var pin = children.FirstOrDefault(x => x.GetType() == typeof(Pushpin)) as Pushpin;        
        pin.DataContext = args.Position.Coordinate;
        //witout binding 
        //pin.GeoCoordinate = args.Position.Coordinate;
    });
}