如何使用必應地圖 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.pswp.cn/news/281159.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/281159.shtml
英文地址,請注明出處:http://en.pswp.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;但沒有時間閱讀所…

scrapy爬蟲實戰分享

自動登錄腳本參考scrapy爬蟲啟示錄-小伙子老夫看你血氣方剛這本《爬蟲秘錄》就傳給你了Scrapy初章-Scrapy理論簡介Scrapy次章-啥也不干就是爬圖Scrapy第四章-設置代理IP偷偷爬圖Scrapy第三章-圖片存庫MysqlScrapy第五章-多線程加速爬圖Scrapy終章-1024福利Scrapy最最最終章-摟一…

【重大更新】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…

onlyoffice采坑筆記

中文版onlyoffice/documentserver鏡像制作onlyoffice 20并發限制處理&#xff0c;up to 20 maximumonlyoffice安裝-Linuxwindows 10 下用docker安裝onlyoffice服務 onlyoffice安裝-Linux 0 點贊 ? 0 回復 ? 3月前onlyoffice相關命令記錄 0 點贊 ? 0 回復 ? 3月前onlyoffice…

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…

用offset調用文章

一。用offset偏移調用文章&#xff0c;這個我認為是比較好的&#xff0c;經常用。容易控制1. 第一個post顯示5篇query_posts(showpost 5); 2.那么第二個post顯示除上面顯示過的6篇query_posts(showpost 6 & offset5); 二、用置頂文章1.第一個Post顯示置頂文章query_posts(s…

MediatRPC - 基于MediatR和Quic通訊實現的RPC框架,比GRPC更簡潔更低耦合,開源發布第一版...

大家好&#xff0c;我是失業在家&#xff0c;正在找工作的博主Jerry。作為一個.Net架構師&#xff0c;就要研究編程藝術&#xff0c;例如SOLID原則和各種設計模式。根據這些原則和實踐&#xff0c;實現了一個更簡潔更低耦合的RPC&#xff08;Remote Procedure Calls&#xff09…

新鮮抓取古文賞析五千篇

新鮮抓取的古文&#xff0c;有感興趣的可以來看看。-IT源點-古文賞析 外科精義 黃景昌-古詩文選集 鼎鐫陳眉公先生批評西廂記 世醫得效方 汪炎昶-古詩文選集 至正條格 樂郊私語 敖氏傷寒金鏡錄 十四經發揮 宋史 草澤狂歌 世醫得效方 : 二十卷. 衛生寶鑒 遼史 陳深-古詩文選集 …

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的自制軟…

Pyhton學習——Day25

#面向對象的幾個方法#1.靜態方法staticmethod&#xff0c;不能訪問類屬性&#xff0c;也不能訪問實例屬性&#xff0c;只是類的工具包#2.類方法&#xff1a;classmethod&#xff0c;在函數屬性前加上類方法&#xff0c;顯示為(cls)代表類&#xff0c;類能訪問到數據屬性和函數屬…

龍芯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;也是對中文字體界面顯示中文不亂碼支持比較好的一個版…

C++中的namespace ----轉載

轉自&#xff1a;http://blog.csdn.net/yao_zhuang/article/details/1853625 namespace中文意思是命名空間或者叫名字空間&#xff0c;傳統的C&#xff0b;&#xff0b;只有一個全局的namespace&#xff0c;但是由于現在的程序的規模越來越大&#xff0c;程序的分工越來越細&am…

使用訪問控制策略訪問服務網格內的服務

當在實施服務網格時&#xff0c;不可避免的存在網格外服務訪問網格內服務的情況&#xff0c;也就是服務網格的平滑落地。這種中間狀態可能會持續較長的時間&#xff0c;也是我們在落地的時候需要解決的問題之一。又或者&#xff0c;有的應用處于某些考慮并不適合使用服務網格&a…

Scrapy從理論到爬圖

Scrapy系列教程&#xff1a; https://www.daxueyiwu.com/post/71 scrapy爬蟲啟示錄-小伙子老夫看你血氣方剛這本《爬蟲秘錄》就傳給你了 0 點贊 ? 0 回復 ? 5月前 Scrapy初章-Scrapy理論簡介 0 點贊 ? 0 回復 ? 5月前 Scrapy次章-啥也不干就是爬圖 0 點贊 ? 0 回復 ? 5月…

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. 在當今艱難的經…

需求工程閱讀筆記03

需求工程與運營商技術管理人員的結合點思考&#xff1b; 運營商的技術管理人員有時會發現自己處于比較尷尬的一個境地&#xff0c;因為一方面自己并不直接開發、實現各種系統&#xff0c;而都是采用外包的方式&#xff0c;業務的需求都是經由自己再轉給外包方&#xff0c;感覺自…

使用BeetleX.MQTT構建服務

已經有很長一段時間沒有寫代碼&#xff0c;為了不讓自己的代碼技能有所下降所以針對BeetleX擴展了一個MQTT協議來保持自己的代碼設計和編寫能力。接下來簡單介紹一下如何使用BeetleX.MQTT來構建對應的TCP或WebSocket服務。 以下實現是針對MQTT 3.1.1版本&#xff0c;協議的實…