如何在 Swift (iOS) 中使用 MKSnapshotter

How to use MKSnapshotter in Swift (iOS)

我目前正在尝试从 MapKit 实现 MKSnapshotter。我想使用苹果地图创建我在地图视图(街景)中找到的位置的快照。

这是我目前的代码:

    var options: MKMapSnapshotOptions = MKMapSnapshotOptions();
    options.region = self.attractionDetailMap.region;
    options.size = self.attractionDetailMap.frame.size;
    options.scale = UIScreen.mainScreen().scale;

    var fileURL:NSURL = NSURL(fileURLWithPath: "path/to/snapshot.png")!;
    var snapshotter:MKMapSnapshotter = MKMapSnapshotter();
    snapshotter.startWithCompletionHandler { (snapshot:MKMapSnapshot!, error:NSError!) -> Void in
        if(error != nil) {
            println("error: " + error.localizedDescription);
            return;
        }
        var image:UIImage = snapshot.image;
        var data:NSData = UIImagePNGRepresentation(image);
        data.writeToURL(fileURL, atomically: true);

        var pin:MKAnnotationView = MKAnnotationView();
        UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale);
        image.drawAtPoint(CGPointMake(0.0, 0.0));
        var rect:CGRect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
        for annotation in self.attractionDetailMap.annotations {
            var point:CGPoint = snapshot.pointForCoordinate(annotation.coordinate);
        }
    }

当我试图提出一个观点时 (var point:CGPoint = snapshot.pointForCoordinate(annotation.coordinate);),错误:'coordinate' 不可用:APIs deprecated as of iOS7 及更早版本在 Swift 中不可用。

基本上tutorials/guidelinesswift中MKSnapshotter的使用方法不多,如果能看到[=]中MKSnapshotter的标准结构,将不胜感激30=]。我觉得我走在正确的道路上,但我被困在最后一行代码,坦率地说,它甚至不起作用。

谢谢!

您需要将注释转换为正确的类型。

for annotation in self.attractionDetailMap.annotations as! [MKAnnotation] {
    let point = snapshot.pointForCoordinate(annotation.coordinate)
}