我在使用 MKMapsnapshotter 时收到错误图像。我尝试手动缩小(设置更高的相机高度),这似乎解决了这个问题,但我不确定如何以编程方式处理这个问题。有没有办法检测到这种错误然后重置相机高度?这是 MapKit 错误吗?当我使用卫星或混合地图类型时会发生这种情况。
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
if (!snapshot.image || error) {
// Can't create snapshot. Use Placeholder
mapImage.image = [UIImage imageNamed:@"NoPhoto.png"];
NSLog(@"Snapshot Error: %@",[error localizedDescription]);
}
任何帮助将不胜感激。谢谢!
编辑:
经过更多的研究和测试,我能够找到一种解决方法。
该错误图像主要发生在海洋/海洋上的卫星图像中。我发现我可以使用反向地理编码器并检查地标数组以查看该位置是否在城市中。如果不是,那么它可能在海洋上空。
// Check location of satellite imagery
CLLocation *location = [[CLLocation alloc] initWithLatitude:myLatitude longitude:myLongitude];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray* placemarks, NSError* error){
// check to see if location is in a city
if ([placemarks[0] locality]) {
// set up camera
MKMapCamera *myCamera = [MKMapCamera
cameraLookingAtCenterCoordinate:location.coordinate
fromEyeCoordinate:location.coordinate
eyeAltitude:250];
// set snapshot options
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.camera = myCamera;
options.showsBuildings = YES;
options.showsPointsOfInterest = NO;
options.mapType = MKMapTypeSatellite;
options.scale = [UIScreen mainScreen].scale;
options.size = myImage.frame.size;
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
if(!error) {
myImage.image = snapshot.image;
}
}]; // end snapshotter completion handler
} else {
// probably in the ocean
myImage.image = [UIImage imageNamed:@"NoPhoto.png"];
}
}];