在 MapControl UWP 中自定义 MapIcon

Customize MapIcon in MapControl UWP

我正在为 UWP 开发应用程序。我的目的是优化 bing map.I 上的地图点渲染,从使用操作 map.children.add() 对自定义标记进行聚类开始。 在引脚集群组之后,我在 xaml 中添加带有生成的依赖对象的引脚。地图上的每个更改位置都会刷新当前显示的所有图钉。 它的工作速度很慢。所以我尝试使用 MapElement.Add()。它工作正常,但我无法添加通用图像 ( xaml ) 代码(_native map是MapControl):

 var mapIcon = new MapIcon();
    mapIcon.Image = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Images/icon.png"));
    mapIcon.Location = new Geopoint(snPosition);
    mapIcon.Title = "Some label".ToString();
    _nativemap.MapElements.Add(mapIcon);

有什么方法可以自定义 mapIcon 的标签(位置、颜色等)或 从 xaml 文件生成流以将其显示为实际的 mapIcon 图像 ??

To add mapicon with image as stream ( generated by xaml ) on map

我不知道你是如何通过xaml生成图像流的,我猜你有一个控件或其他东西并且你使用RenderTargetBitmap to generate a image source which is populated with the combined contents of a XAML visual tree. Then you can use BitmapEncoder创建一个InMemoryRandomAccessStream

为了演示如何使用BitmapEncoder,我这里以官方MapControl sample的场景1为例:

MapIcon 创建一个 SymbolIcon 并创建一个 Button 以将 MapIcon 放在地图上:

<Grid x:Name="imgContainer" Margin="0,5" Width="20" Height="25">
    <SymbolIcon Symbol="AddFriend" Foreground="Red" />
</Grid>
<Button Content="Place MapIcon" Click="Button_Click" />

后面的代码:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    //render symbolicon to bmp
    RenderTargetBitmap renderbmp = new RenderTargetBitmap();
    await renderbmp.RenderAsync(imgContainer);

    using (InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream())
    {
        //create a bitmap encoder
        BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
        //write pixels into this encoder
        var pixels = await renderbmp.GetPixelsAsync();
        var reader = DataReader.FromBuffer(pixels);
        byte[] bytes = new byte[reader.UnconsumedBufferLength];
        reader.ReadBytes(bytes);
        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight,
            (uint)renderbmp.PixelWidth, (uint)renderbmp.PixelHeight, 0, 0, bytes);
        await encoder.FlushAsync();
        mapIconStreamReference = RandomAccessStreamReference.CreateFromStream(stream);

        //create mapIcon
        var mapIcon = new MapIcon();
        mapIcon.Image = mapIconStreamReference;
        mapIcon.Location = new Geopoint(myMap.Center.Position);
        mapIcon.Title = "Some label".ToString();
        myMap.MapElements.Add(mapIcon);
    }
}

此演示的渲染图像: