在MapControl UWP

时间:2016-10-18 14:37:02

标签: c# win-universal-app bing uwp-maps

我正在为UWP开发应用程序。我的目的是在bing map上对地图点渲染进行optymalize。我从使用map.children.add()操作聚类自定义标记开始。 在群集组的引脚之后,我在xaml中添加了生成的dependencyobject的引脚。地图上的每个更改位置都会刷新当前显示的所有引脚。 它的工作很慢。所以我尝试使用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图像??

1 个答案:

答案 0 :(得分:0)

  

在地图

上添加带图像的mapicon作为流(由xaml生成)

我不知道你是如何通过xaml生成图像流的,我猜你有一个控件或者什么东西,你使用RenderTargetBitmap生成一个图像源,其中填充了一个组合的内容XAML可视化树。然后,您可以使用BitmapEncoder创建InMemoryRandomAccessStream

为了演示如何使用BitmapEncoder,我在此处采用官方MapControl sample的方案1:

SymbolIcon创建MapIcon并创建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);
    }
}

渲染此演示的图像: enter image description here

相关问题