如何拍摄某些坐标的地图快照?现在它拍摄了整张地图的快照,但我想拍摄NewYork(40.7127°N,74.0059°)的快照。我尝试将region.center更改为纽约坐标,但我一直在旧金山。
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.region = self.mapView.region;
options.scale = [UIScreen mainScreen].scale;
options.size = self.mapView.frame.size;
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
UIImage *image = snapshot.image;
NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:[self snapshotFilename] atomically:YES];
}];
答案 0 :(得分:0)
CLLocationCoordinate2D
参数,在您的情况下,位置协调。
CLLocationCoordinate2D centerPoint = CLLocationCoordinate2DMake(40.730872, -74.003066);
在下面的方法中传递此坐标,它将捕获指定的位置。如果需要,可以从此方法中删除动画,它与功能无关,但最好将动画地图显示到特定位置。看看这个并分享你的想法。
- (void) takeSnapShot:(CLLocationCoordinate2D)centerPoint {
__block MKCoordinateRegion newRegion = self.mapView.region;
// Animation is use to animate map to location specified in parameter. If not required remove it.
[UIView animateWithDuration:1.5 animations:^{
MKCoordinateRegion region;
region.center.latitude = centerPoint.latitude;
region.center.longitude = centerPoint.longitude;
region.span.latitudeDelta = 0.15f;
region.span.longitudeDelta = 0.15f;
newRegion = region;
[self.mapView setRegion:region animated:YES];
} completion:^(BOOL finished) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"Snap.png"];
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.region = newRegion;
options.scale = [UIScreen mainScreen].scale;
options.size = self.mapView.frame.size;
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
UIImage *image = snapshot.image;
NSData *data = UIImagePNGRepresentation(image);
[data writeToFile:fileName atomically:YES];
}];
}];
}