[英]UWP MapControl highlight countries
我正在寫一個小應用程序,它將收到一個國家代碼(2個ISO字母)和一個州代碼(也2個ISO字母)。
我想突出顯示(並塗上顏色)這兩個信息指定的區域(所以說“ CA,QC”將突出顯示加拿大的魁北克州)
我不需要任何其他東西(好吧,也許是FadeIn,FadeOut動畫,但我稍后會弄清楚)
所有縮放/點擊/單擊/其他操作均被阻止。
MapControl聲明非常簡單:
<maps:MapControl Grid.Row="1" x:Name="myMap" ZoomLevel="0"/>
提前致謝
編輯:經過大量研究,以下回答為您提供了幫助,我很驚訝BASIC動作不是Microsoft平台的一部分。 太瘋狂了 所有后端都在不到30分鍾的時間內進行了編碼(包括身份驗證,列表屬性,檢查訪問級別,設置SignalR回調),但是從視覺上來講,我們並沒有從UWP平台獲得任何幫助。 真是可悲。 /再見UWP,我已經嘗試過了。 多次。
編輯2:使其經過一些調整即可工作:
if (feature != null && (feature.Geometry.Type == GeoJSONObjectType.Polygon) || (feature.Geometry.Type == GeoJSONObjectType.MultiPolygon))
{
myMap.MapElements.Clear();
MapPolygon polygon = null;
if (feature.Geometry.Type == GeoJSONObjectType.Polygon)
{
var polygonGeometry = feature.Geometry as Polygon;
polygon = new MapPolygon
{
Path = new Geopath(polygonGeometry.Coordinates[0].Coordinates.Select(coord => new BasicGeoposition() { Latitude = coord.Latitude, Longitude = coord.Longitude })),
FillColor = Colors.DarkRed
};
myMap.MapElements.Add(polygon);
}
else
{
var ploy = (feature.Geometry as MultiPolygon);
foreach (var item in ploy.Coordinates)
{
var polygon1 = new MapPolygon
{
Path = new Geopath(item.Coordinates[0].Coordinates.Select(coord => new BasicGeoposition() { Latitude = coord.Latitude, Longitude = coord.Longitude })),
FillColor = Colors.DarkRed
};
myMap.MapElements.Add(polygon1);
}
}
}
沒有內置的方法可以實現此目的,因此您將不得不執行一些附加步驟來完成此工作。
首先,您需要下載一個基於geojson
的數據集,其中包含所有國家/地區的多邊形定義。 可以在GitHub上找到一種輕量級的功能。
現在,您需要從NuGet安裝GeoJSON.NET包到您的項目中,並將下載的.geojson
文件包括在您的項目中,例如Assets
文件夾中。 確保其Build操作設置為Content 。
現在,您可以使用以下代碼通過創建MapPolygon
並將其放置在地圖上來突出顯示一個國家:
private FeatureCollection _countryPolygons = null;
private async void HighlightClick(string country)
{
if (_countryPolygons == null)
{
_countryPolygons = JsonConvert.DeserializeObject<FeatureCollection>(
await FileIO.ReadTextAsync(
await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/countries.geojson",
UriKind.Absolute))));
}
var feature = _countryPolygons.Features.FirstOrDefault(f =>
f.Id.Equals(country, StringComparison.CurrentCultureIgnoreCase));
if (feature != null && feature.Geometry.Type == GeoJSONObjectType.Polygon)
{
var polygonGeometry = feature.Geometry as Polygon;
MapPolygon polygon = new MapPolygon();
polygon.Path = new Geopath(polygonGeometry.Coordinates[0].Coordinates.Select(coord => new BasicGeoposition() { Latitude = coord.Latitude, Longitude = coord.Longitude }));
polygon.FillColor = Colors.DeepSkyBlue;
Map.MapElements.Clear();
Map.MapElements.Add(polygon);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.