如何使用必应地图 WPF 控件

 如何使用必应地图 WPF 控件

如何使用必应地图 WPF 控件

作者:WPFDevelopersOrg - 驚鏵

原文链接:https://github.com/WPFDevelopersOrg/WPFDevelopers

  • 框架使用.NET40

  • Visual Studio 2019;

  • Bing Maps WPF 控件需要 .NET Framework 4.0Windows 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 代码如下:

using Microsoft.Maps.MapControl.WPF;namespace WPFDevelopers.Samples.ExampleViews
{public class PushpinModel{public Location Location { get; set; }public int ID { get; set; }public string Title { get; set; }}
}

3)新建 BingAMapExample.xaml 代码如下:

<UserControl x: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><LinearGradientBrush x:Key="MainColor" EndPoint="0.5,1" StartPoint="0.5,0"><GradientStop Color="#FF020621" Offset="0"/><GradientStop Color="#FF364BD8" Offset="1"/><GradientStop Color="#FF5A6BD8" Offset="0.499"/></LinearGradientBrush><ControlTemplate x:Key="CarTemplate" TargetType="map:Pushpin"><Grid ToolTip="物资车辆"><Ellipse Width="35" Height="35" Fill="White" StrokeThickness="3" Stroke="Red"/><Image Source="pack://application:,,,/WPFDevelopers.Samples;component/Images/AMap/Car2.png" Width="25" Height="25"/></Grid></ControlTemplate><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 SineEaseOut}" /><DoubleAnimation Storyboard.TargetProperty="ScaleY" Storyboard.TargetName="Scale"Duration="0:0:01" To="2" EasingFunction="{StaticResource SineEaseOut}" /><DoubleAnimation Storyboard.TargetProperty="Opacity"  Duration="0:0:01" To="0" EasingFunction="{StaticResource SineEaseOut}" /></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></UserControl.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:AMapTitleLayer/><map:MapItemsControl ItemsSource="{Binding PushpinArray,RelativeSource={RelativeSource AncestorType=local:BingAMapExample}}"><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:MapLayer x:Name="CarLayer"/></map:Map><DockPanel><Grid DockPanel.Dock="Top"><Path Data="M75.811064,0 L275.81107,0 275.81107,39.999999 267.12714,39.999999 267.12798,40.000499 67.127973,40.000499 0,0.00050140842 75.811064,0.00050140842 z"Width="200" Height="30" Stretch="Fill" Opacity=".8" Stroke="#FFCCFEFF" StrokeThickness="1.5" Fill="{StaticResource MainColor}"><Path.RenderTransform><SkewTransform AngleX="-40"/></Path.RenderTransform></Path><TextBlock Text="预警指挥平台" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center"FontSize="16" FontWeight="Black"/></Grid><Grid DockPanel.Dock="Left" Width="200"HorizontalAlignment="Left" ><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition/><RowDefinition Height="Auto"/></Grid.RowDefinitions><Rectangle Grid.RowSpan="3" Fill="#FF364BD8" Opacity=".8" Stroke="#FFCCFEFF" StrokeThickness="1.5"/><TextBlock Text="数据信息" FontSize="16" Foreground="White" Margin="10,10,0,0"/><ItemsControl ItemsSource="{Binding PushpinArray,RelativeSource={RelativeSource AncestorType=local:BingAMapExample}}"Margin="4,10" Grid.Row="1"><ItemsControl.ItemTemplate><DataTemplate><Grid Margin="4" x:Name="PART_Grid"MouseLeftButtonDown="PART_Grid_MouseLeftButtonDown"Tag="{Binding ID}"><Grid.Resources><Style TargetType="TextBlock"><Setter Property="Foreground" Value="White"/><Setter Property="FontSize" Value="16"/></Style></Grid.Resources><Grid.ColumnDefinitions><ColumnDefinition Width="Auto"/><ColumnDefinition/></Grid.ColumnDefinitions><Ellipse Width="20" Height="20" Fill="OrangeRed"/><TextBlock Text="{Binding ID}" HorizontalAlignment="Center"/><TextBlock Margin="4,0" Grid.Column="1" Text="{Binding Title}"/></Grid><DataTemplate.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="Background" TargetName="PART_Grid" Value="#FF020621"/></Trigger></DataTemplate.Triggers></DataTemplate></ItemsControl.ItemTemplate></ItemsControl><Button Content="物资轨迹" Click="BtnCar_Click" Grid.Row="2" Height="40" Style="{StaticResource PrimaryButton}"/></Grid></DockPanel></Grid>
</UserControl>

4)新建 BingAMapExample.xaml.cs 逻辑代码如下:

  • 点击左侧列表的point点地图定位到并放大层级到16级。

using Microsoft.Maps.MapControl.WPF;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;namespace WPFDevelopers.Samples.ExampleViews
{/// <summary>/// BingAMapExample.xaml 的交互逻辑/// </summary>public partial class BingAMapExample : UserControl{private LocationCollection _polyLocations;private MapPolyline mapPolyline;private Pushpin carPushpin;private DispatcherTimer dispatcherTimer;private List<Location> locations;public IEnumerable PushpinArray{get { return (IEnumerable)GetValue(PushpinArrayProperty); }set { SetValue(PushpinArrayProperty, value); }}public static readonly DependencyProperty PushpinArrayProperty =DependencyProperty.Register("PushpinArray", typeof(IEnumerable), typeof(BingAMapExample), new PropertyMetadata(null));public BingAMapExample(){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.9219204792284, 116.203500574855), Title = "石景山山姆会员超市" });pushpins.Add(new PushpinModel { ID = 4, Location = new Location(39.9081417418219, 116.331244439925), Title = "茂林居小区" });PushpinArray = pushpins;_polyLocations = new LocationCollection();_polyLocations.Add(new Location(39.9082973053021, 116.63105019548));_polyLocations.Add(new Location(31.9121578992881, 107.233555852083));mapPolyline = new MapPolyline{Stroke = Brushes.Green,StrokeThickness = 2,Locations = _polyLocations,};CarLayer.Children.Add(mapPolyline);carPushpin = new Pushpin{Template = this.Resources["CarTemplate"] as ControlTemplate,Location = new Location(31.9121578992881, 107.233555852083),PositionOrigin = PositionOrigin.Center,};CarLayer.Children.Add(carPushpin);dispatcherTimer = new DispatcherTimer();dispatcherTimer.Interval = TimeSpan.FromSeconds(1.5);dispatcherTimer.Tick += DispatcherTimer_Tick;}int index = 0;private void DispatcherTimer_Tick(object sender, EventArgs e){if (index < 0){index = locations.Count - 1;dispatcherTimer.Stop();return;}carPushpin.Location = locations[index];index--;}private void BtnCar_Click(object sender, RoutedEventArgs e){locations = new List<Location>();locations.Add(new Location(39.9082973053021, 116.63105019548));locations.Add(new Location(39.0654365763652, 115.513103745601));locations.Add(new Location(38.5861378332358, 114.897869370601));locations.Add(new Location(38.0690298850334, 114.238689683101));locations.Add(new Location(37.4436424646135, 113.491619370601));locations.Add(new Location(36.8833163124675, 112.832439683101));locations.Add(new Location(36.6015984304246, 112.480877183101));locations.Add(new Location(36.2125510101126, 112.041424058101));locations.Add(new Location(35.6074752751952, 111.426189683101));locations.Add(new Location(34.9977887035825, 110.591228745601));locations.Add(new Location(34.456028305434, 109.932049058101));locations.Add(new Location(33.9836399832877, 109.580486558101));locations.Add(new Location(33.5086116028286, 108.965252183101));locations.Add(new Location(33.1046158275268, 108.525799058101));locations.Add(new Location(32.6617655474571, 108.042400620601));locations.Add(new Location(32.179523137361, 107.515056870601));locations.Add(new Location(31.9121578992881, 107.233555852083));index = locations.Count - 1;dispatcherTimer.Start();}private void Map_MouseDown(object sender, MouseButtonEventArgs e){Point mousePosition = e.GetPosition(this);Location pinLocation = this.map.ViewportPointToLocation(mousePosition);}private void Pushpin_MouseDown(object sender, MouseButtonEventArgs e){var model = sender as Pushpin;map.Center = model.Location;map.ZoomLevel = 16;}private void PART_Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e){var grid = sender as Grid;var model = PushpinArray.OfType<PushpinModel>().FirstOrDefault(x => x.ID.Equals(grid.Tag));map.Center = model.Location;map.ZoomLevel = 16;}}
}

c3937e3c347c7d385570d1186190723b.gif

Github|BingAMapExample[1]
码云|BingAMapExample[2]

参考资料

[1]

Github|BingAMapExample: https://github.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers.Samples/ExampleViews/Map/BingAMapExample.xaml

[2]

码云|BingAMapExample: https://gitee.com/WPFDevelopersOrg/WPFDevelopers/blob/master/src/WPFDevelopers.Samples/ExampleViews/Map/BingAMapExample.xaml

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/281159.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

如何保存推特链接以供以后从台式机和手机阅读

Have you come across a lot of interesting links from Twitter, but you don’t have the time to read all of them? Today we’ll show you how to read these links later from your desktop and phone. 您是否遇到过Twitter上很多有趣的链接&#xff0c;但没有时间阅读所…

【重大更新】DevExpress v17.2新版亮点—Bootstrap篇(二)

用户界面套包DevExpress v17.2日前终于正式发布&#xff0c;本站将以连载的形式为大家介绍各版本新增内容。本文将介绍了Bootstrap Controls v17.2 的CardView、Charts、Editors、GridView、Layout等新功能&#xff0c;快来下载试用新版本&#xff01; GridView Toolbar 在此版…

盘点 .NET 7 新功能

点击上方蓝字关注我们&#xff08;本文阅读时间&#xff1a;20分钟)本文翻译于 Jeremy Likness, Angelos Petropoulos 和 Jon Douglas 的博客.NET 7 为C# 11/F# 7、.NET MAUI、ASP.NET Core/Blazor、Web API、WinForms、WPF 等应用程序带来了更高的性能和新功能。使用 .NET 7&a…

nb-iot链路层加密_Google为低端Android手机和IoT设备创建了更快的加密

nb-iot链路层加密Google谷歌Low-resource Android phones and IoT devices don’t have the processing power to use modern encryption services, which makes them vulnerable to hacking. That’s why Google is introducing Adiantum, a super-fast encryption standard f…

MediatRPC - 基于MediatR和Quic通讯实现的RPC框架,比GRPC更简洁更低耦合,开源发布第一版...

大家好&#xff0c;我是失业在家&#xff0c;正在找工作的博主Jerry。作为一个.Net架构师&#xff0c;就要研究编程艺术&#xff0c;例如SOLID原则和各种设计模式。根据这些原则和实践&#xff0c;实现了一个更简洁更低耦合的RPC&#xff08;Remote Procedure Calls&#xff09…

wii拆机_设置防砖保护以保护和增强Wii

wii拆机We’ve shown you how to hack your Wii for homebrew software, emulators, and DVD playback, now it’s time to safeguard your Wii against bricking and fix some annoyances—like that stupid “Press A” health screen. 我们已经向您展示了如何破解Wii的自制软…

龙芯IPC追平Zen2 稳步推进产业生态

日前&#xff0c;2022年信息技术自主创新高峰论坛在南京成功召开&#xff0c;来自政府、产业、各行业领域的领导、专家学者、企业代表齐聚长江之滨&#xff0c;共话信息产业自主创新发展。铁流谈谈会上几个亮点。龙芯LA664追平AMD zen2相对于一些技术引进CPU在引进海外技术后CP…

摄像头水平视野垂直视野?_如何在“动物穿越:新视野”中的梦中游览某人的岛屿...

摄像头水平视野垂直视野?The promised second summer update for Animal Crossing: New Horizons has been released, and it restores the ability to visit another player’s island in your dreams. Before you can do so, though, you’ll need a Nintendo Online member…

中文版onlyoffice镜像制作

原文同步于&#xff1a;https://www.daxueyiwu.com/post/761 拉取5.4.2.46版本onlyoffice/documentserver镜像 docker pull onlyoffice/documentserver:5.4.2.46 该版本是支持20个连接数破解限制的最后一个版本&#xff0c;也是对中文字体界面显示中文不乱码支持比较好的一个版…

使用访问控制策略访问服务网格内的服务

当在实施服务网格时&#xff0c;不可避免的存在网格外服务访问网格内服务的情况&#xff0c;也就是服务网格的平滑落地。这种中间状态可能会持续较长的时间&#xff0c;也是我们在落地的时候需要解决的问题之一。又或者&#xff0c;有的应用处于某些考虑并不适合使用服务网格&a…

gfi截图_GFI Backup Home Edition是Windows的免费数据备份实用程序

gfi截图In today’s tough economic times the last thing you want is to lose important data because you couldn’t afford a quality backup utility. Today we look at GFI Backup Home Edition, a completely free professional grade backup solution. 在当今艰难的经…

使用BeetleX.MQTT构建服务

已经有很长一段时间没有写代码&#xff0c;为了不让自己的代码技能有所下降所以针对BeetleX扩展了一个MQTT协议来保持自己的代码设计和编写能力。接下来简单介绍一下如何使用BeetleX.MQTT来构建对应的TCP或WebSocket服务。 以下实现是针对MQTT 3.1.1版本&#xff0c;协议的实…

vlc 视频流跳数_如何解决在播放高清晰度视频文件的VLC中跳过和滞后的问题

vlc 视频流跳数VLC is the king of all media… it plays almost anything on any platform, any time, any place. It’s great. Lately, however, I’ve been having issues with VLC skipping whenever I’m playing high-def media streaming over a network. VLC是所有媒体…

onlyoffice中文字体下载

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/778 下划线开头的是页面显示中文的字体&#xff0c;不带下划线的是页面显示英文的字体 calibri.ttf -CalibriTimes New Roman.ttf _FANGSONG.otf -仿宋FANGSONG.otf_FS_GB2312.otf -仿宋_GB2312_HWZS.otf -华文…

onlyoffice 20并发限制处理

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/31 开源版本连续打开20个页面就会弹出该提示。 一.方案一 修改六个app.js文件 find ./ -name app.js 目录下所有app.js文件&#xff0c;对代码行进行修改&#xff0c;只是经过简单的测试&#xff0c;希望发现…

onlyoffice更新中文字体总结

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/760 1. 通过字体修改软件FontCreator修改字体名称 &#xff08;1&#xff09;下载fontcreat.exe &#xff08;2&#xff09;将Windows/fonts 下的字体文件拷贝至 我的文档/fonts (3) 使用fontcreat打开字体…

ubuntu 键盘快捷键_如何使用键盘快捷键在Ubuntu中提高生产力

ubuntu 键盘快捷键diceareawesome1/Shutterstock.comdiceareawesome1 / Shutterstock.comWe’re always looking for new ways to speed up everyday tasks in Ubuntu. We’ll show you some keyboard shortcuts you might not have known about, and show you how to make you…

Ubuntu 桌面系统升级

本文介绍 Ubuntu 桌面系统升级的两种方式&#xff0c;通过 UI 或命令行的方式&#xff0c;演示为 20.04 升级为 22.04。并介绍了 windows 的 Linux 子系统 wsl 的升级注意事项。背景之前在学习 ROS2 时&#xff0c;安装 ros-humble-desktop 出现依赖错误&#xff1a;无法修正错…

onlyoffice修改左上角的logo

原文同步自作者博客&#xff1a;https://www.daxueyiwu.com/post/770 1. 商用版config里配置就能修改logo "editorConfig": {"customization": {"logo": {"image": "https://example.com/logo.png","imageEmbedded&q…

pidgin qq_Pidgin入门指南,通用消息客户端

pidgin qqIf you find chatting with multiple chat clients troublesome, then Pidgin is the tool for you. In today’s article, we’ll show you how to connect to popular chat networks, encrypt your conversations, and render mathematical formula in Pidgin. 如果…