iOS 7では、MKMap Snapshotterを使用します。 NSHipster から:
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.region = self.mapView.region;
options.size = self.mapView.frame.size;
options.scale = [[UIScreen mainScreen] scale];
NSURL *fileURL = [NSURL fileURLWithPath:@"path/to/snapshot.png"];
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
if (error) {
NSLog(@"[Error] %@", error);
return;
}
UIImage *image = snapshot.image;
NSData *data = UIImagePNGRepresentation(image);
[data writeToURL:fileURL atomically:YES];
}];
iOS7の前に次のいずれかを実行します。
- ライブマップを追加してインタラクションを無効にします。
- Google Static Maps APIを使用します(下記参照)。
- MKMapViewを作成し、イメージにレンダリングします。私はこれを試しました(以前の編集を参照)が、タイルを読み込むことができませんでした。 needLayoutやその他のメソッドを呼び出そうとしましたが、うまくいきませんでした。
Apple's deal with Google is more reliable than a free API, but on the bright side, it is really simple. Documentation is at http://code.google.com/apis/maps/documentation/staticmaps/
This is the minimum amount of parameters for practical use:
http://maps.googleapis.com/maps/api/staticmap?center=40.416878,-3.703530&zoom=15&size=290x179&sensor=false
パラメータの概要:
center
: this can be an address, or a latitude,longitude pair with up to 6 decimals (more than 6 are ignored).
format
: png8 (default), png24, git, jpg, jpg-baseline.
language
: Use any language. Default will be used if requested wasn't available.
maptype
: One of the following: roadmap, satellite, hybrid, terrain.
scale
: 1,2,4. Use 2 to return twice the amount of pixels on retina displays. 4 is restricted to premium customers.
Non paid resolution limits are 640x640 for 1 and 2.
sensor
: true or false. Mandatory. It indicates if the user is being located using a device.
size
: size in pixels.
zoom
: 0 (whole planet) to 21.
ポリゴン、カスタムマーカーを追加したり、マップをスタイルしたりするには、さらにいくつかの方法があります。
NSData* data = [NSData dataWithContentsOfURL:@"http://maps.googleap..."];
UIImage *img = [UIImage imageWithData:data];