我创建图片图片的代码如下:
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.region = MKCoordinateRegionMakeWithDistance(userLocation, 300, 300) ;
options.size = CGSizeMake(320,180);
options.scale = [UIScreen mainScreen].scale;
MKMapSnapshotter *snapShotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapShotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
//
if (error) {
NSLog(@"err = %@",[error description]);
}
UIImage *img = snapshot.image;
MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:nil];
CGPoint coordinatePoint = [snapshot pointForCoordinate:userLocation];
coordinatePoint.x += pin.centerOffset.x - (CGRectGetWidth(pin.bounds) / 2.0);
coordinatePoint.y += pin.centerOffset.y - (CGRectGetHeight(pin.bounds) / 2.0);
UIGraphicsBeginImageContextWithOptions(img.size, YES, img.scale);
{
[img drawAtPoint:CGPointZero];
[pin.image drawAtPoint:coordinatePoint];
_imgViewMapThubbnail.image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
_imgViewMapThubbnail.layer.cornerRadius = 10 ;
_imgViewMapThubbnail.layer.masksToBounds = true ;
}];
此代码为普通红色引脚,位置几乎正确。我想要我的自定义引脚,所以我添加了如下代码
pin.image = [UIImage imageNamed:@"MY_IMAGE_NAME"];
图像图像已更改,其工作正常。但是,我的位置有一些区别。我不知道如何调整代码或固定图像大小以将其设置到正确的位置?
答案 0 :(得分:1)
看到这是一个老问题,对于那些来自这里的自定义引脚图像的人来说,这里是上面示例的修改代码,可以将引脚呈现在正确的位置。
CGPoint coordinatePoint = [snapshot pointForCoordinate:newCenter];
UIImage *pin = [UIImage imageNamed:@"pin"];
CGPoint centerOffset = CGPointMake(0, -pin.size.height / 2);
coordinatePoint.x += centerOffset.x - (pin.size.width / 2.0);
coordinatePoint.y += centerOffset.y - (pin.size.height / 2.0);
UIGraphicsBeginImageContextWithOptions(img.size, YES, img.scale);
{
[img drawAtPoint:CGPointZero];
[pin drawAtPoint:coordinatePoint];
_imgViewMapThubbnail.image = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();