none
Cannot draw map tiles by setting MapTileBitmapRequest.PixelData (IRandomAccessStreamReference) RRS feed

  • Question

  • I am trying to use the CustomMapTileDataSource.BitmapRequested event to set the MapTileBitmapRequest.PixelData property. But despite following several examples (and proving that my code will work with BitmapImage.SetSource), the tiles are not drawn on the map.

    private async void CustomMapTileDataSource_OnBitmapRequested(CustomMapTileDataSource sender, MapTileBitmapRequestedEventArgs args)
            {
    
                MapTileBitmapRequestDeferral deferral = args.Request.GetDeferral();
                args.Request.PixelData = await GetBitmapAsync(args);
                deferral.Complete();
            }


    private async Task<RandomAccessStreamReference> GetBitmapAsync(MapTileBitmapRequestedEventArgs args)
            {
                try
                {
                    string tileUri = string.Format(_uriFormatString, args.ZoomLevel, args.X, args.Y);
    
                    using (Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient())
                    {
                        IInputStream tileInputStream = await httpClient.GetInputStreamAsync(new Uri(tileUri));
    
                        using (Stream tileStream = tileInputStream.AsStreamForRead())
                        {
                            //Stream does not support seeking: use an intermediary stream
                            using (MemoryStream memoryStream = new MemoryStream())
                            {
                                await tileStream.CopyToAsync(memoryStream);
    
                                //results in exception: Additional information: This IRandomAccessStream does not support the CloneStream method because it requires cloning and this stream does not support cloning.
                                //memoryStream.Position = 0;
                                //return RandomAccessStreamReference.CreateFromStream(memoryStream.AsRandomAccessStream());
    
                                //no exception, but nothing is drawn
                                byte[] tileBuffer = new byte[memoryStream.Length];
                                memoryStream.Read(tileBuffer, 0, (int)memoryStream.Length);
    
                                InMemoryRandomAccessStream inMemoryRandomAccessStream = new InMemoryRandomAccessStream();
                                IOutputStream outputStream = inMemoryRandomAccessStream.GetOutputStreamAt(0);
    
                                DataWriter writer = new DataWriter(outputStream);
                                writer.WriteBytes(tileBuffer);
                                await writer.StoreAsync();
                                await writer.FlushAsync();
    
                                return RandomAccessStreamReference.CreateFromStream(inMemoryRandomAccessStream);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string exception = ex.ToString();
    
                    if (Debugger.IsAttached)
                        Debugger.Break();
    
                    return null;
                }
            }

    If I use a BitmapImage, it appears correctly. 

    InMemoryRandomAccessStream inMemoryRandomAccessStream = new InMemoryRandomAccessStream(); IOutputStream outputStream = inMemoryRandomAccessStream.GetOutputStreamAt(0); byte[] tileBuffer = new byte[memoryStream.Length]; memoryStream.Read(tileBuffer, 0, (int)memoryStream.Length);

    DataWriter writer = new DataWriter(outputStream); writer.WriteBytes(tileBuffer); await writer.StoreAsync(); await writer.FlushAsync();

    BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(inMemoryRandomAccessStream); TestImage.Source = bitmapImage;


    • Moved by Anne Jing Monday, January 5, 2015 7:51 AM
    Friday, January 2, 2015 6:23 PM

Answers

All replies

  • Hi,

    I move your thread to the Bing Maps for Windows Store apps forum . You can get more professional help in there!

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Monday, January 5, 2015 7:50 AM
  • Take a look at my code sample here: https://code.msdn.microsoft.com/Adding-Opacity-and-WMS-cf6773f1 It basically downloads a tile from a URL and then edits the pixel data for the opacity (you can skip this step if you want).


    http://rbrundritt.wordpress.com

    Tuesday, January 6, 2015 11:10 PM