2016-10-18 32 views
0

为UWP开发应用程序。我的目的是optymalize在bing map上渲染地图点。我从使用map.children.add()操作对自定义标记进行聚类开始。 集群组的引脚后,我添加引脚与xaml中生成的dependencyobject。地图上的每个更改位置刷新当前显示的所有引脚。 它的工作非常缓慢。所以我试图使用MapElement.Add()。它工作正常,但我不能添加普通图像(XAML) 代码(_native地图是地图控件):在MapControl中自定义MapIcon UWP

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图像?

+0

你的代码没有问题,你想要什么?在xaml代码中添加mapicon并且不在代码后面生成它? –

+0

在地图上添加带有图像的mapicon作为流(由xaml生成) – user3045261

回答

0

要在地图上

与图像流(通过XAML生成的)添加mapicon我不知道你是怎么生成的XAML一个图像流,我想你有一个控制或东西,您使用RenderTargetBitmap生成一个图像源,该图像源用XAML可视化树的组合内容填充。然后你可以使用BitmapEncoder创建一个InMemoryRandomAccessStream

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

创建为MapIcon一个SymbolIcon并创建一个ButtonMapIcon上图:

<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