0

我正在尝试在我的 WinRT 应用程序中为 Bing 地图创建自定义图钉。我的问题是我需要从我的页面中引用实际地图,以便在我的 userControl 中正确固定图标。因此,例如,这是我的 DataTemplate,它绑定到地图并适用于普通图钉。为了使我的自定义 userControl 正确定位,我需要在 userControl 中引用父 Map。

这是我的 XAML:

<m:MapItemsControl x:Name="Pushpinss" ItemsSource="{Binding InventoryItems}">
        <m:MapItemsControl.ItemTemplate>
          <DataTemplate>
            <!-- NORMAL PUSHPIN WORKS -->
            <m:Pushpin>
              <m:MapLayer.Position>
                <m:Location Latitude="{Binding WarehouseLatitude}"
                            Longitude="{Binding WarehouseLongitude}" />
              </m:MapLayer.Position>
            </m:Pushpin>
            <!-- CUSTOM CONTROL DISPLAYS BUT DOES NOT POSITION CORRECTLY BECAUSE I NEED A REFERENCE TO THE MAP-->
            <View:GPSIcon  Latitude="{Binding WarehouseLatitude}"
                           Longitude="{Binding WarehouseLongitude}"
                           Radius="100000"/>
              <x:Arguments>
              </x:Arguments>
          </DataTemplate>
        </m:MapItemsControl.ItemTemplate>
      </m:MapItemsControl>

这是我的自定义控件:

public sealed partial class GPSIcon : UserControl
  {
    private Map _map;
    private const double EARTH_RADIUS_METERS = 6378137;

    public GPSIcon(Map map)
    {
      this.InitializeComponent();

      _map = map;
      _map.ViewChanged += (s, e) =>
      {
        UpdateAccuracyCircle();
      };
    }

    public static readonly DependencyProperty LatitudeProperty =
           DependencyProperty.Register("Latitude", typeof(double), typeof(GPSIcon), new PropertyMetadata(0));

    public static readonly DependencyProperty LongitudeProperty =
        DependencyProperty.Register("Longitude", typeof(double), typeof(GPSIcon), new PropertyMetadata(0));

    public static readonly DependencyProperty RadiusProperty =
       DependencyProperty.Register("Radius", typeof(double), typeof(GPSIcon), new PropertyMetadata(0));

    public double Latitude
    {
      get { return (double)GetValue(LatitudeProperty); }
      set { SetValue(LatitudeProperty, value); }
    }

    public double Longitude
    {
      get { return (double)GetValue(LongitudeProperty); }
      set { SetValue(LongitudeProperty, value); }
    }

    /// <summary>
    /// Radius in Metres
    /// </summary>
    public double Radius
    {
      get { return (double)GetValue(RadiusProperty); }
      set
      {
        SetValue(RadiusProperty, value);
        UpdateAccuracyCircle();
      }
    }

    private void UpdateAccuracyCircle()
    {
      if (_map != null && Radius >= 0)
      {
        double groundResolution = Math.Cos(_map.Center.Latitude * Math.PI / 180) * 2 * Math.PI * EARTH_RADIUS_METERS / (256 * Math.Pow(2, _map.ZoomLevel));
        double pixelRadius = Radius / groundResolution;

        AccuracyCircle.Width = pixelRadius;
        AccuracyCircle.Height = pixelRadius;
        AccuracyCircle.Margin = new Thickness(-pixelRadius / 2, -pixelRadius / 2, 0, 0);
      }
    }
  }

这可能吗?我也尝试过使用这里描述的 x:Arguments 指令:http: //msdn.microsoft.com/en-us/library/ee795382.aspx

谢谢

4

2 回答 2

0

更新 1

进行以下更改

1)添加空构造函数。

public GPSIcon()
{
    this.InitializeComponent();
}

2)声明类型的DPMap

public Map MyMap
{
    get { return (Map)GetValue(MyMapProperty); }
    set { SetValue(MyMapProperty, value); }
}

public static readonly DependencyProperty MyMapProperty =
    DependencyProperty.Register("MyMap", typeof(Map), typeof(GPSIcon), new PropertyMetadata(default(Map), OnMapSet));

private static void OnMapSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    _map = ((GPSIcon)(d)).MyMap;
    _map.ViewChanged += (ss, ee) =>
    {
        ((GPSIcon)(d)).UpdateAccuracyCircle();
    };
}

Map3)在 XAML 中传递这样的对象

<m:Map x:Name="objMap">
    <m:MapItemsControl x:Name="Pushpinss" ItemsSource="{Binding InventoryItems}">
        <m:MapItemsControl.ItemTemplate>
            <DataTemplate>
                <View:GPSIcon Latitude="{Binding WarehouseLatitude}"
                              Longitude="{Binding WarehouseLongitude}"
                              Radius="100000"
                              MyMap="{Binding ElementName=objMap}"/>
            </DataTemplate>
        </m:MapItemsControl.ItemTemplate>
    </m:MapItemsControl>
</m:Map>

声明另一个类型的依赖属性,Map然后您应该将当前地图实例作为该 DP 的值传递给<View:GPSIcon ... />

简单地说,您需要遵循与如何在 Silverlight 中将参数从 xaml 传递给构造函数相同的逻辑

于 2013-08-29T10:58:13.990 回答
0

为了让您的自定义 UIElement 在地图上正确定位,您可以做的而不是在代码中执行此操作只需像设置图钉位置一样设置 UIElement 的位置。

例如:

<View:GPSIcon Radius="100000">
    <m:MapLayer.Position>
       <m:Location Latitude="{Binding WarehouseLatitude}"
                   Longitude="{Binding WarehouseLongitude}" />
    </m:MapLayer.Position>
</View:GPSIcon>
于 2013-08-29T12:58:20.593 回答