【问题标题】:Bing maps custom pushpin not showing without moving the mapBing 地图自定义图钉在不移动地图的情况下不显示
【发布时间】:2012-12-23 22:58:21
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    您是否尝试过为地图图钉使用自定义控件

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

    例子:

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

    在哪里,

    1. CustomPushPin 您的自定义图钉用户控件。
    2. location 属于 Location 类。

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

    【讨论】:

      【解决方案2】:

      这是一个令人沮丧的问题。

      我最终得到的解决方案是在添加图钉时创建摆动效果。 每当我更新我的图钉列表时,我都会更改 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));
              }
          }
      }
      

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

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

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多