详解WPF如何使用必应地图控件
如何使用必应地图 wpF 控件
- 框架使用
.NET40
; Visual Studio 2019
;Bing Maps WPF
控件需要.NET Framework 4.0
和Windows SDK
。这两个组件都随Microsoft Visual Studio VisualC#
或Visual Basic Express Edition
一起安装。- 下面演示如何使用。
- 引用
Microsoft.Maps.MapControl.WPF.dll
也可以在nuget上找到。 - 添加命名空间。
- 新建
AMapTitleLayer.cs
继承图层MapTileLayer
实现加载高德地图瓦片.
1)新建 AMapTitleLayer.cs
代码如下:
using Microsoft.Maps.MapControl.WPF; using System; namespace WPFDevelopers.Samples.ExampleViews { public class AMapTitleLayer : MapTileLayer { public AMapTitleLayer() { TileSource = new AMapTileSource(); } public string UriFormat { get { return TileSource.UriFormat; } set { TileSource.UriFormat = value; } } } public class AMapTileSource : TileSource { public override Uri GetUri(int x, int y, int zoomLevel) { string url = string.Format("http://wprd01.is.autonavi.com/appmaptile?x={0}&y={1}&z={2}&lang=zh_cn&size=1&scl=1&style=7", x, y, zoomLevel); return new Uri(url, UriKind.Absolute); } } }
2)新建PushpinModel.cs
代码如下:
usingMicrosoft.Maps.MapControl.WPF; namespaceWPFDevelopers.Samples.ExampleViews { publicclassPushpinModel { publicLocationLocation{get;set;} publicintID{get;set;} publicstringTitle{get;set;} } }
3)新建 BingAMapExample.xaml
代码如下:
<UserControlx:Class="WPFDevelopers.Samples.ExampleViews.BingAMapExample" XMLns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:WPFDevelopers.Samples.ExampleViews" xmlns:map="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF" xmlns:mapOverlays="clr-namespace:Microsoft.Maps.MapControl.WPF.Overlays;assembly=Microsoft.Maps.MapControl.WPF" mc:Ignorable="d" d:DesignHeight="450"d:DesignWidth="800"> <UserControl.Resources> <LinearGradientBrushx:Key="MainColor"EndPoint="0.5,1"StartPoint="0.5,0"> <GradientStopColor="#FF020621"Offset="0"/> <GradientStopColor="#FF364BD8"Offset="1"/> <GradientStopColor="#FF5A6BD8"Offset="0.499"/> </LinearGradientBrush> <ControlTemplatex:Key="CarTemplate"TargetType="map:Pushpin"> <GridToolTip="物资车辆"> <EllipseWidth="35"Height="35"Fill="White"StrokeThickness="3"Stroke="Red"/> <ImageSource="pack://application:,,,/WPFDevelopers.Samples;component/Images/AMap/Car2.png"Width="25"Height="25"/> </Grid> </ControlTemplate> <SineEasex:Key="SineOut"EasingMode="Easeout"/> <Storyboardx:Key="AnimateRound"RepeatBehavior="Forever"> <DoubleAnimationStoryboard.TargetProperty="ScaleX"Storyboard.TargetName="Scale" Duration="0:0:01"To="2" EasingFunction="{StaticResourceSineEaseOut}"/> <DoubleAnimationStoryboard.TargetProperty="ScaleY"Storyboard.TargetName="Scale" Duration="0:0:01"To="2" EasingFunction="{StaticResourceSineEaseOut}"/> <DoubleAnimationStoryboard.TargetProperty="Opacity" Duration="0:0:01"To="0" EasingFunction="{StaticResourceSineEaseOut}"/> </Storyboard> <Stylex:Key="alarmStyle"TargetType="map:Pushpin"> <SetterProperty="PositionOrigin"Value="Center"/> <SetterProperty="Width"Value="60"/> <SetterProperty="Height"Value="60"/> <SetterProperty="Template"> <Setter.Value> <ControlTemplateTargetType="map:Pushpin"> <Grid> <EllipseHeight="40"Width="40"Fill="Red"RenderTransformOrigin="0.5,0.5"> <Ellipse.OpacityMask> <RadialGradientBrush> <GradientStopOffset="0"Color="Transparent"/> <GradientStopOffset="1"Color="Black"/> </RadialGradientBrush> </Ellipse.OpacityMask> <Ellipse.RenderTransform> <ScaleTransformx:Name="Scale"/> </Ellipse.RenderTransform> <Ellipse.Triggers> <EventTriggerRoutedEvent="Loaded"> <BeginStoryboardStoryboard="{StaticResourceAnimateRound}"> </BeginStoryboard> </EventTrigger> </Ellipse.Triggers> </Ellipse> <ViewboxWidth="30"Height="30" VerticalAlignment="Top" HorizontalAlignment="Center" Margin="20,0,0,0"> <PathData="{StaticResourcePathFlag}"Fill="Orange"/> </Viewbox> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources> <Grid> <map:Mapx:Name="map"ZoomLevel="5"Center="39.9132801985722,116.392009995601" CredentialsProvider="AgXB7m7fVYxKpjEZV9rGdrRPvLgawYhi4Wvw99kk4RDspoalC3B_vQ8GKJAoxrve"> <map:Map.Mode> <map:MercatorMode/> </map:Map.Mode> <local:AMapTitleLayer/> <map:MapItemsControlItemsSource="{BindingPushpinArray,RelativeS编程客栈ource={RelativeSourceAncestorType=local:BingAMapExample}}"> <map:MapItemsControl.ItemTemplate> <DataTemplate> <map:PushpinLocation="{BindingLocation}"Cursor="Hand" MouseDown="Pushpin_MouseDown" ToolTip="{BindingTitle}" Background="Red"> <TextblockText="{BindingID}"/> </map:Pushpin> </DataTemplate> </map:MapItemsControl.ItemTemplate> </map:MapItemsControl> <map:PushpinLocation="36.6797276003243,118.495410536117"Style="{StaticResourcealarmStyle}"/> <CanvasWidth="50"Height="80" map:MapLayer.Position="31.9121578992881,107.233555852083" map:MapLayer.PositionOrigin="BottomCenter"Opacity="0.7"> <PathData="M0,0L50,050,5025,800,500,0"Fill="ForestGreen"Stroke="Wheat"StrokeThickness="2"/> <TextBlockFontSize="10"Foreground="White"Padding="10"TextAlignment="Center"> 这里是<LineBreak/> 四川<LineBreak/> 通江县<LineBreak/> </TextBlock> </Canvas> <map:MapLayerx:Name="CarLayer"/> </map:Map> <DockPanel> <GridDockPanel.Dock="Top"> <PathData="M75.811064,0L275.81107,0275.81107,39.999999267.12714,39.999999267.12798,40.00049967.127973,40.0004990,0.0005014084275.811064,0.00050140842z" Width="200"Height="30"Stretch="Fill"Opacity=".8"Stroke="#FFCCFEFF"StrokeThickness="1.5"Fill="{StaticResourceMainColor}"> <Path.RenderTransform> <SkewTransformAngleX="-40"/> </Path.RenderTransform> </Path> <TextBlockText="预警指挥平台"Foreground="White编程客栈" VerticalAlignment="Center"HorizontalAlignment="Center" FontSize="16"FontWeight="Black"/> </Grid> <GridDockPanel.Dock="Left"Width="200" HorizontalAlignment="Left"> <Grid.RowDefinitions> <RowDefinitionHeight="Auto"/> <RowDefinition/> <RowDefinitionHeight="Auto"/> </Grid.RowDefinitions> <RectangleGrid.RowSpan="3"Fill="#FF364BD8"Opacity=".8"Stroke="#FFCCFEFF"StrokeThickness="1.5"/> <TextBlockText="数据信息"FontSize="16"Foreground="White"Margin="10,10,0,0"/> <ItemsControlItem编程sSource="{BindingPushpinArray,RelativeSource={RelativeSourceAncestorType=local:BingAMapExample}}" Margin="4,10"Grid.Row="1"> <ItemsControl.ItemTemplate> <DataTemplate> <GridMargin="4"x:Name="PART_Grid" MouseLeftButtonDown="PART_Grid_MouseLeftButtonDown" Tag="{BindingID}"> <Grid.Resources> <StyleTargetType="TextBlock"> <SetterProperty="Foreground"Value="White"/> <SetterProperty="FontSize"Value="16"/> </Style> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinitionWidth="Auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <EllipseWidth="20"Height="20"Fill="OrangeRed"/> <TextBlockText="{BindingID}"HorizontalAlignment="Center"/> <TextBlockMargin="4,0"Grid.Column="1"Text="{BindingTitle}"/> </Grid> <DataTemplate.Triggers> <TriggerProperty="IsMouseOver"Value="True"> <SetterProperty="Background"TargetName="PART_Grid"Value="#FF020621"/> </Trigger> </DataTemplate.Triggers> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> <ButtonContent="物资轨迹"Click="BtnCar_Click"Grid.Row="2"Height="40"Style="{StaticResourcePrimaryButton}"/> </Grid> </DockPanel> </Grid> </UserControl>
4)新建 BingAMapExample.xaml.cs
逻辑代码如下:
点击左侧列表的point
点地图定开发者_Python入门位到并放大层级到16级。
usingMicrosoft.Maps.MapControl.WPF; usingSystem; usingSystem.Collections; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Windows; usingSystem.Windows.Controls; usingSystem.Windows.Input; usingSystem.Windows.Media; usingSystem.Windows.Threading; namespaceWPFDevelopers.Samples.ExampleViews { ///<summary> ///BingAMapExample.xaml的交互逻辑 ///</summary> publicpartialclassBingAMapExample:UserControl { privateLocationCollection_polyLocations; privateMapPolylinemapPolyline; privatePushpincarPushpin; privateDispatcherTimerdispatcherTimer; privateList<Location>locations; publicIEnumerablePushpinArray { get{return(IEnumerable)GetValue(PushpinArrayProperty);} set{SetValue(PushpinArrayProperty,value);} } publicstaticreadonlyDependencyPropertyPushpinArrayProperty= DependencyProperty.Register("PushpinArray",typeof(IEnumerable),typeof(BingAMapExample),newPropertyMetadata(null)); publicBingAMapExample() { InitializeComponent(); varpushpins=newList<PushpinModel>(); pushpins.Add(newPushpinModel{ID=1,Location=newLocation(39.8151940395589,116.411970893135),Title="和义东里社区"}); pushpins.Add(newPushpinModel{ID=2,Location=newLocation(39.9094878843105,116.33299936282),Title="中国水科院南小区"}); pushpins.Add(newPushpinModel{ID=3,Location=newLocation(39.9219204792284,116.203500574855),Title="石景山山姆会员超市"}); pushpins.Add(newPushpinModel{ID=4,Location=newLocation(39.9081417418219,116.331244439925),Title="茂林居小区"}); PushpinArray=pushpins; _polyLocations=newLocationCollection(); _polyLocations.Add(newLocation(39.9082973053021,116.63105019548)); _polyLocations.Add(newLocation(31.9121578992881,107.233555852083)); mapPolyline=newMapPolyline { Stroke=Brushes.Green, StrokeThickness=2, Locations=_polyLocations, }; CarLayer.Children.Add(mapPolyline); carPushpin=newPushpin { Template=this.Resources["CarTemplate"]asControlTemplate, Location=newLocation(31.9121578992881,107.233555852083), PositionOrigin=PositionOrigin.Center, }; CarLayer.Children.Add(carPushpin); dispatcherTimer=newDispatcherTimer(); dispatcherTimer.Interval=TimeSpan.FromSeconds(1.5); dispatcherTimer.Tick+=DispatcherTimer_Tick; } intindex=0; privatevoidDispatcherTimer_Tick(objectsender,EventArgse) { if(index<0) { index=locations.Count-1; dispatcherTimer.Stop(); return; } carPushpin.Location=locations[index]; index--; } privatevoidBtnCar_Click(objectsender,RoutedEventArgse) { locations=newList<Location>(); locations.Add(newLocation(39.9082973053021,编程客栈116.63105019548)); locations.Add(newLocation(39.0654365763652,115.513103745601)); locations.Add(newLocation(38.5861378332358,114.897869370601)); locations.Add(newLocation(38.0690298850334,114.238689683101)); locations.Add(newLocation(37.4436424646135,113.491619370601)); locations.Add(newLocation(36.8833163124675,112.832439683101)); locations.Add(newLocation(36.6015984304246,112.480877183101)); locations.Add(newLocation(36.2125510101126,112.041424058101)); locations.Add(newLocation(35.6074752751952,111.426189683101)); locations.Add(newLocation(34.9977887035825,110.591228745601)); locations.Add(newLocation(34.456028305434,109.932049058101)); locations.Add(newLocation(33.9836399832877,109.580486558101)); locations.Add(newLocation(33.5086116028286,108.965252183101)); locations.Add(newLocation(33.1046158275268,108.525799058编程客栈101)); locations.Add(newLocation(32.6617655474571,108.042400620601)); locations.Add(newLocation(32.179523137361,107.515056870601)); locations.Add(newLocation(31.9121578992881,107.233555852083)); index=locations.Count-1; dispatcherTimer.Start(); } privatevoidMap_MouseDown(objectsender,MouseButtonEventArgse) { PointmousePosition=e.GetPosition(this); LocationpinLocation=this.map.ViewportPointToLocation(mousePosition); } privatevoidPushpin_MouseDown(objectsender,MouseButtonEventArgse) { varmodel=senderASPushpin; map.Center=model.Location; map.ZoomLevel=16; } privatevoidPART_Grid_MouseLeftButtonDown(objectsender,MouseButtonEventArgse) { vargrid=senderasGrid; varmodel=PushpinArray.OfType<PushpinModel>().FirstOrDefault(x=>x.ID.Equals(grid.Tag)); map.Center=model.Location; map.ZoomLevel=16; } } }
效果
到此这篇关于详解WPF如何使用必应地图控件的文章就介绍到这了,更多相关WPF必应地图控件内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
精彩评论