xamarin iOS:使用叠加层映射快照图像

xamarin iOS: Map Snapshot image with overlays

我正在保存图像快照,但我的叠加层未保存。

甚至可以用地图快照保存叠加层吗?我有两点之间的路线和一些图像。

我每次移动地图区域时都会保存地图,这样我就知道叠加层已经渲染(正如我在屏幕上看到的那样):

    private void SetMapCenter()
    {
        if (Map.Overlays.Count() > 0)
            _mapHelper.SetMapSnapshpot(Map);
    }

    public void SetMapSnapshpot(MKMapView map)
    {
        using (var snapShotOptions = new MKMapSnapshotOptions())
        {
            snapShotOptions.Region = map.Region;
            snapShotOptions.Scale = UIScreen.MainScreen.Scale;
            snapShotOptions.Size = map.Frame.Size;

            using (var snapShot = new MKMapSnapshotter(snapShotOptions))
            {
                snapShot.Start((snapshot, error) =>
                {
                    if (error == null)
                    {
                        snapshot.Image.SaveToPhotosAlbum(
                            (uiimage, imgError) =>
                        {
                            if (imgError == null)
                            {
                                new UIAlertView("Image Saved", "Map View Image Saved!", null, "OK", null).Show();
                            }
                        });
                    }
                });
            }
        }
    }

不幸的是你必须自己在生成的图像上绘制它们:

Snapshot images do not include any custom overlays or annotations that your app added to the map view. If you want your annotations and overlays to appear on the final image, you must draw them yourself. To position those items correctly on the image, use the pointForCoordinate: method of this class to translate the overlay or annotation coordinate value to an appropriate location inside the image’s coordinate space.

参考:https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapSnapshot_class/index.html#//apple_ref/doc/uid/TP40013271

NOTE

Snapshotter objects do not capture the visual representations of any overlays or annotations that your app creates. If you want those items to appear in the final snapshot, you must draw them on the resulting snapshot image. For more information about drawing custom content on map snapshots, see MKMapSnapshot Class Reference.

参考:https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKMapSnapshotter_class/index.html

我使用此博客 post 示例 (ObjC) 在 C# 中构建了我自己的示例:

请参阅 Drawing Annotations on Map View Snapshot 部分及其下面的部分以获取示例,并根据您实际需要在保存的地图图像中包含的内容进行转换:

http://nshipster.com/mktileoverlay-mkmapsnapshotter-mkdirections/