【问题标题】:Bind map center and pushpin in windows phone 8 application在 windows phone 8 应用程序中绑定地图中心和图钉
【发布时间】:2015-07-01 18:57:53
【问题描述】:

我正在开发一个 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>

【问题讨论】:

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


【解决方案1】:

您想在 Windows Phone 8 应用程序中使用 Bing 地图吗?我在 WP8 应用程序中使用旧的 Bing 地图控件时遇到问题。我写了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>

//初始化图钉

private Pushpin MyPushpin { get; set; }

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

// 启动地理定位器

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

【讨论】:

  • 请给我一个更好的解决方案。
  • 我认为 ObservableCollection children = MapExtensions.GetChildren(NokiaMap); var pin = children.FirstOrDefault(x => x.GetType() == typeof(Pushpin)) 作为图钉; MyPushpin = 引脚;也有助于 bing 地图
  • 在您的代码中 pushpin1.DataContext = args.Position.Coordinate;不,你不能以这种方式使用 MapExtensions.Children 中的图钉
  • 你能纠正我在stackoverflow.com/questions/18759491/…给出的代码
猜你喜欢
  • 1970-01-01
  • 2017-01-12
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多