0

我有一个支持 Windows Phone 平台的通用解决方案。用户点击必应地图后,会创建一个图钉,同时还会创建地图的快照,但此图像不包含图钉。

我正在将 Windows.UI.Xaml.Controls.Maps.MapControl 与此 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));
}
4

1 回答 1

0

由于向地图添加数据是异步操作,在向地图添加任何数据(例如 MapIcon)或使用任何视图设置 API 更改地图视图后,您应该等到地图控件的 LoadingStatus 变为 MapLoadingStatus .加载。这表明地图控件已完成异步工作并完全呈现。您可以在地图控件上注册 LoadingStatusChanged 事件。

于 2018-06-11T18:27:22.493 回答