必应地图自定义图钉不在不移动地图的情况下显示

5

我正在使用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 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>

一旦我移除Pushpin的自定义样式,使用默认样式的Pushpin会正确显示而无需移动地图。我希望自定义的Pushpin也能不需要手动移动地图就能类似地显示出来。提前感谢您的解决方案。

2个回答

1
这是一个令人沮丧的问题。
我最终采用的解决方案是,在添加我的图钉时创建摆动效果。 每当我更新我的图钉列表时,我会更改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));
        }
    }
}

这会自动移动视图,使得图钉弹出。
这有点取巧,但至少它能够起作用。此外,它还有一个附带效果,可以微微弹出视图,向用户展示视图已经发生了变化。
希望这能有所帮助。

1

您尝试过使用自定义控件来设置地图标记吗?

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

例如:

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

其中,

  1. CustomPushPin 是您的自定义标记用户控件。
  2. location 是 Location 类型的类。

如果您仍然遇到问题,请告诉我。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接