Bing在没有移动地图的情况下映射自定义图钉

时间:2012-12-23 22:58:21

标签: c# xaml windows-8 bing-maps pushpin

我正在使用C#和XAML开发Windows 8应用程序。该应用程序有一个地图页面,其中包含自定义图钉。我使用以下代码添加了自定义图钉:

    <Style x:Key="PushPinStyle" TargetType="bm:Pushpin">
        <Setter Property="Width" Value="25"/>
        <Setter Property="Height" Value="39"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Image Source="Assets/pushpin_icon.png" Stretch="Uniform" HorizontalAlignment="Left"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>

启用上述代码后,除非用户移动了地图,否则图钉不会显示。以下代码用于生成地图。 的DataTemplate -

    <DataTemplate x:Key="pushpinSelector" >
        <bm:Pushpin Tapped="pushpinTapped" Style="{StaticResource PushPinStyle}">
            <bm:MapLayer.Position >
                <bm:Location Latitude="{Binding Latitude}" Longitude="{Binding Longitude}"/>
            </bm:MapLayer.Position>
        </bm:Pushpin>
    </DataTemplate>

地图XAML-             

            <bm:Map.Children>
                <bm:MapItemsControl Name="pushPinModelsLayer" 
                     ItemsSource="{Binding Results}" 
                     ItemTemplate="{StaticResource pushpinSelector}" />
            </bm:Map.Children>
        </bm:Map>

删除图钉的自定义样式后,默认图钉会正确显示而无需移动地图。我希望cutom图钉能够同样显示,而无需手动移动地图。感谢提前解决方案。

2 个答案:

答案 0 :(得分:1)

您是否尝试过使用地图图钉

的自定义控件

使用提供的用户控件模板创建控件,添加必要的组件,事件,进行所需的自定义。

示例:

CustomPushPin pushpin = new CustomPushPin(); mapView.Children.Add(pushPin); MapLayer.SetPosition(pushPin, location);

其中,

  1. CustomPushPin您的自定义图钉用户控件。
  2. location的类型为Location。
  3. 如果您仍然面临问题,请告诉我

答案 1 :(得分:1)

这是令人沮丧的事情。

我最终得到的解决方案是每当我添加图钉时创建一个Wiggle效果。 每当我更新我的图钉列表时,我都会更改MapBounds(通过自定义依赖属性)。

在这个方法中,我稍微弹出地图边界,然后放大到所需的边界,如下所示:

public static LocationRect GetMapBounds(DependencyObject obj)
{
    return (LocationRect)obj.GetValue(MapBoundsProperty);
}

public static void SetMapBounds(DependencyObject obj, LocationRect value)
{
    obj.SetValue(MapBoundsProperty, value);
}

public static readonly DependencyProperty MapBoundsProperty = DependencyProperty.RegisterAttached("MapBounds", typeof(LocationRect), typeof(MapBindings), new PropertyMetadata(null, OnMapBoundsChanged));

private static void OnMapBoundsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var map = d as Bing.Maps.Map;
    if (map != null)
    {
        // sigh.  "Wiggle" the view to force map pins to appear
        LocationRect destRect = e.NewValue as LocationRect;
        if (destRect != null)
        {
            LocationRect wiggleRect = new LocationRect(destRect.Center, destRect.Width + 0.001,
                                                        destRect.Height + 0.001);

            map.SetView(wiggleRect, MapAnimationDuration.None);
            map.SetView(destRect, new TimeSpan(0, 0, 1));
        }
    }
}

这会导致视图自动移动,使图钉弹出。

这有点像黑客,但至少它有效。此外,它还具有弹出视图的副作用,以向用户显示视图已更改。

希望有所帮助。

相关问题