我创建引脚图像的代码如下:
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,所以我添加了如下代码
pin.image = [UIImage imageNamed:@"MY_IMAGE_NAME"];引脚图像已更改,并且工作正常。然而,我的位置有一些不同。我不知道,如何调整我的代码或引脚图像大小,以设置到正确的位置?
发布于 2016-09-26 11:33:04
考虑到这是一个老问题,对于任何来这里定制引脚图像的人来说,这里有一个来自上面示例的修订代码,它可以将引脚渲染到正确的位置。
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();https://stackoverflow.com/questions/33755097
复制相似问题