I want to use custom markers, but it centered in annotation.coordinate.
According to this example: https://www.mapbox.com/ios-sdk/examples/marker-image/
I do next:
-(MGLAnnotationImage *)mapView:(MGLMapView *)mapView imageForAnnotation:(id<MGLAnnotation>)annotation
{
MARMapMarker *marker = [self markerForAnnotation:annotation];
NSString *reuseID = marker.title;
MGLAnnotationImage *img = [mapView dequeueReusableAnnotationImageWithIdentifier:reuseID];
if (!img)
{
UIImage *iconImage = [marker.icon imageWithAlignmentRectInsets:UIEdgeInsetsMake(0, 0, marker.icon.size.height / 2, 0)];
img = [MGLAnnotationImage annotationImageWithImage:iconImage reuseIdentifier:reuseID];
}
return img;
}
but it doesn't work.
Any suggestions?
Platform: IOS
Mapbox SDK version: 3.2.1
move anchor point to image bottom
nothing happend
Currently, the alignment rect insets only affect hit testing, so that for instance you can make the bottom half of your annotation image untappable. This can come in handy for your use case: since all annotations are currently centered on the image鈥檚 center, pad your image with whitespace so that the image鈥檚 center point lines up with the desired visual center point. For example, in this example, the image asset is twice as tall as the tower; the bottom half is completely transparent. But the alignment rect ensures that tapping the bottom half of the image doesn鈥檛 cause the callout view to appear.
This particular issue is being tracked in #2151, and we鈥檝e got plans to make custom annotations a lot more intuitive: #4392.
I'm using the following hack instead to achieve "bottom" anchoring:
MGLAnnotationImage *annotationImage = [self->_mapView dequeueReusableAnnotationImageWithIdentifier:@"marker"];
if (annotationImage == nil) {
UIImage *initialImage = [UIImage imageNamed:@"map-marker"];
// scale image down so it looks good on retina
UIImage *scaledImage = [UIImage imageWithCGImage:[initialImage CGImage] scale:(initialImage.scale * 2) orientation:(initialImage.imageOrientation)];
// add padding at the bottom
UIGraphicsBeginImageContextWithOptions(CGSizeMake(scaledImage.size.width, scaledImage.size.height * 2), NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
[scaledImage drawAtPoint:CGPointMake(0, 0)];
UIGraphicsPopContext();
UIImage *imageWithPadding = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// add insets so that bottom padding is not interactable
UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, imageWithPadding.size.height/2, 0);
UIImage *imageWithPaddingAndInsets = [imageWithPadding imageWithAlignmentRectInsets:insets];
annotationImage = [MGLAnnotationImage annotationImageWithImage:imageWithPaddingAndInsets reuseIdentifier:@"marker"];
}
return annotationImage;
Most helpful comment
I'm using the following hack instead to achieve "bottom" anchoring: