iosxcodexamarinmkmapviewoverlays

xamarin iOS: Map Snapshot image with overlays


I am saving an image snapshot but my overlays aren't getting saved.

Is it even possible to save the overlays with a map snapshot? I have routes between two points and as also some images.

I save the map every time I move the map region so that I know the overlays have been rendered (as I can see them on screen):

    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();
                            }
                        });
                    }
                });
            }
        }
    }

Solution

  • Unfortunately you have to draw them yourself on the resulting image:

    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.

    Ref: 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.

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

    I used this blog post example (ObjC) to build my own in C#:

    See the Drawing Annotations on Map View Snapshot section and the sections below it for examples and convert based upon what you actually need to include on your saved map images:

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