0

I have a universal solution with platform supporting Windows Phone. After the user has tapped on Bing Maps, a pin is created an snapshot of the map is also created, but this image doesn't contain the pin.

I am using Windows.UI.Xaml.Controls.Maps.MapControl with this xaml:-

XAML:

<mp:MapControl x:Name="mpWinPh"
                           Grid.Row="1"
                           Width="{Binding ElementName=ControlRoot,
                                           Path=Width}"
                           Height="{StaticResource ActivityLogGfxSize}"
                           MapServiceToken="{Binding Credentials}"
                           MapTapped="mpWinPh_MapTapped" />

C#:

private async void mpWinPh_MapTapped(MapControl sender, MapInputEventArgs args)
{
    var location = args.Location;
    BasicGeoposition snPosition = new BasicGeoposition
    {
        Latitude = location.Position.Latitude,
        Longitude = location.Position.Longitude,
        Altitude = location.Position.Altitude
    };
    Geopoint snPoint = new Geopoint(snPosition);
    MapIcon icon = new MapIcon { Location = snPoint };
    mpWinPh.Center = snPoint;
    mpWinPh.ZoomLevel = 15;
    mpWinPh.MapElements.Clear();
    mpWinPh.MapElements.Add(icon);

    Field.GpsCoordinate coordinate = new Field.GpsCoordinate { Latitude = location.Position.Latitude, Longitude = location.Position.Longitude };
    (viewModel as LocationControlViewModel).UpdateGps(coordinate);

    await SaveImage();
}

private async Task SaveImage()
{
    var renderTargetBitmap = new RenderTargetBitmap();
    await renderTargetBitmap.RenderAsync(mpWinPh);
    var pixelBuffer = await renderTargetBitmap.GetPixelsAsync();

    var pixels = pixelBuffer.ToArray();

    // Useful for rendering in the correct DPI
    var displayInformation = DisplayInformation.GetForCurrentView();

    var stream = new InMemoryRandomAccessStream();
    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream);
    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                         BitmapAlphaMode.Premultiplied,
                         (uint)renderTargetBitmap.PixelWidth,
                         (uint)renderTargetBitmap.PixelHeight,
                         displayInformation.RawDpiX,
                         displayInformation.RawDpiY,
                         pixels);

    await encoder.FlushAsync();
    stream.Seek(0);
    byte[] resizedData = new byte[stream.Size];
    await stream.ReadAsync(resizedData.AsBuffer(), (uint)stream.Size, InputStreamOptions.None);
    await SaveStreamToTempLocation(() => Task.FromResult(new MemoryStream(resizedData) as Stream));
}
Federico Navarrete
  • 3,069
  • 5
  • 41
  • 76
Mamta
  • 39
  • 1
  • 7

1 Answers1

0

Since adding data to the map is an asynchronous operation, after adding any data to the map (such as a MapIcon), or changing the map view with any of the view setting API's, you should wait until the LoadingStatus of the map control becomes MapLoadingStatus.Loaded. This indicates the map control has finished the asynch work and is fully rendered. You can register for the LoadingStatusChanged event on the map control.

Duncan Lawler
  • 1,772
  • 8
  • 13
  • I could save image with pin by adding pin to map control before saving. All works fine when it is in page. But when this MapControl was moved to pop up. I get an exception on line await renderTargetBitmap.RenderAsync(mpWinPh) saying "value does not fall within expected range." Any help is highly appreciated ! – Mamta Jun 27 '18 at 06:04