2016-10-18 29 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