WPF实现Map加载
以下文章来源于WPF开发者 ,作者驚鏵
效果预览:
一、MainWindow.xaml代码如下:
<Window x:Class="WpfBingMap.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
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"
xmlns:PresentationOptions="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:local="clr-namespace:WpfBingMap"
mc:Ignorable="d"
Title="地图" Width="1024" Height="768">
<Window.Resources>
<Geometry x:Key="PathFlag" PresentationOptions:Freeze="True">M687.5 125C500 125 375 13.7 187.5 62.5V31.3C187.5 31.3 187.5 0 156.3 0C125 0 125 31.3 125 31.3V1000H187.5V625C375 562.5 500 687.5 687.5 687.5C875 687.5 937.5 625 937.5 625V62.5C937.5 62.5 875 125 687.5 125Z</Geometry>
<SineEase x:Key="SineOut" EasingMode="EaseOut" />
<Storyboard x:Key="AnimateRound" RepeatBehavior="Forever">
<DoubleAnimation Storyboard.TargetProperty="ScaleX" Storyboard.TargetName="Scale"
Duration="0:0:01" To="2"
EasingFunction="{StaticResource SineOut}" />
<DoubleAnimation Storyboard.TargetProperty="ScaleY" Storyboard.TargetName="Scale"
Duration="0:0:01" To="2"
EasingFunction="{StaticResource SineOut}" />
<DoubleAnimation Storyboard.TargetProperty="Opacity"
Duration="0:0:01" To="0"
EasingFunction="{StaticResource SineOut}" />
</Storyboard>
<Style x:Key="alarmStyle" TargetType="map:Pushpin">
<Setter Property="PositionOrigin" Value="Center"/>
<Setter Property="Width" Value="60"/>
<Setter Property="Height" Value="60"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="map:Pushpin">
<Grid>
<Ellipse Height="40" Width="40" Fill="Red" RenderTransformOrigin="0.5,0.5">
<Ellipse.OpacityMask>
<RadialGradientBrush>
<GradientStop Offset="0" Color="Transparent" />
<GradientStop Offset="1" Color="Black" />
</RadialGradientBrush>
</Ellipse.OpacityMask>
<Ellipse.RenderTransform>
<ScaleTransform x:Name="Scale"/>
</Ellipse.RenderTransform>
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard Storyboard="{StaticResource AnimateRound}">
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
<Viewbox Width="30" Height="30"
VerticalAlignment="Top"
HorizontalAlignment="Center"
Margin="20,0,0,0">
<Path Data="{StaticResource PathFlag}" Fill="Orange"/>
</Viewbox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<map:Map x:Name="map" ZoomLevel="5" Center="39.9132801985722,116.392009995601"
CredentialsProvider="AgXB7m7fVYxKpjEZV9rGdrRPvLgawYhi4Wvw99kk4RDspoalC3B_vQ8GKJAoxrve">
<!--<map:Map.Mode>
<map:MercatorMode/>
</map:Map.Mode>
<local:OpenstreetmapTileLayer UriFormat="https://tile.openstreetmap.org/{z}/{x}/{y}.png"/>-->
<map:MapItemsControl ItemsSource="{Binding PushpinArray,RelativeSource={RelativeSource AncestorType=local:MainWindow}}">
<map:MapItemsControl.ItemTemplate>
<DataTemplate>
<map:Pushpin Location="{Binding Location}" Cursor="Hand"
MouseDown="Pushpin_MouseDown"
ToolTip="{Binding Title}"
Background="Red">
<TextBlock Text="{Binding ID}"/>
</map:Pushpin>
</DataTemplate>
</map:MapItemsControl.ItemTemplate>
</map:MapItemsControl>
<map:Pushpin Location="36.6797276003243,118.495410536117" Style="{StaticResource alarmStyle}"/>
<Canvas Width="50" Height="80"
map:MapLayer.Position="31.9121578992881,107.233555852083"
map:MapLayer.PositionOrigin="BottomCenter" Opacity="0.7">
<Path Data="M 0,0 L 50,0 50,50 25,80 0,50 0,0" Fill="ForestGreen" Stroke="Wheat" StrokeThickness="2" />
<TextBlock FontSize="10" Foreground="White" Padding="10" TextAlignment="Center">
这里是 <LineBreak />
四川 <LineBreak />
通江县 <LineBreak />
</TextBlock>
</Canvas>
</map:Map>
</Grid>
</Window>
二、MainWindow.xaml.cs代码如下:
public partial class MainWindow : Window
{
public IEnumerable PushpinArray
{
get { return (IEnumerable)GetValue(PushpinArrayProperty); }
set { SetValue(PushpinArrayProperty, value); }
}
public static readonly DependencyProperty PushpinArrayProperty =
DependencyProperty.Register("PushpinArray", typeof(IEnumerable), typeof(MainWindow), new PropertyMetadata(null));
public MainWindow()
{
InitializeComponent();
var pushpins = new List<PushpinModel>();
pushpins.Add(new PushpinModel { ID=1, Location = new Location(39.8151940395589, 116.411970893135),Title="和义东里社区" });
pushpins.Add(new PushpinModel { ID = 2, Location = new Location(39.9094878843105, 116.33299936282) ,Title="中国水科院南小区"});
pushpins.Add(new PushpinModel { ID = 3, Location = new Location(39.9181518802641, 116.203328913478),Title="石景山山姆会员超市" });
pushpins.Add(new PushpinModel { ID = 4, Location = new Location(39.9081417418219, 116.331244439925), Title = "茂林居小区" });
PushpinArray = pushpins;
}
private void Pushpin_MouseDown(object sender, MouseButtonEventArgs e)
{
var model = sender as Pushpin;
map.Center = model.Location;
map.ZoomLevel = 16;
}
}
public class PushpinModel
{
public Location Location { get; set; }
public int ID { get; set; }
public string Title { get; set; }
}
public class OpenstreetmapTileSource: TileSource
{
public override Uri GetUri(int x, int y, int zoomLevel)
{
var uri= new Uri(UriFormat.
Replace("{x}", x.ToString()).
Replace("{y}", y.ToString()).
Replace("{z}", zoomLevel.ToString()));
Console.WriteLine(uri);
return uri;
}
}
public class OpenstreetmapTileLayer : MapTileLayer
{
public OpenstreetmapTileLayer()
{
TileSource = new OpenstreetmapTileSource();
}
public string UriFormat
{
get { return TileSource.UriFormat; }
set { TileSource.UriFormat = value; }
}
}
源码地址
github:https://github.com/yanjinhuagood/WPFBingMap
gitee:https://gitee.com/yanjinhua/WPFBingMap.git
ArcGIS加载瓦片图源码地址
github:https://github.com/yanjinhuagood/ArcGISMap
WPF开发者QQ群: 340500857
blogs: https://www.cnblogs.com/yanjinhua
Github:https://github.com/yanjinhuagood
出处:https://www.cnblogs.com/yanjinhua
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
转载请著名作者 出处 https://github.com/yanjinhuagood
赞 (0)