Mapbox-gl-native: MGLAnnotationImage anchor point not changing

Created on 28 Apr 2016  路  2Comments  路  Source: mapbox/mapbox-gl-native

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

Expected behavior

move anchor point to image bottom

Actual behavior

nothing happend

iOS

Most helpful comment

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;

All 2 comments

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;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

melihcolpan picture melihcolpan  路  3Comments

fousa picture fousa  路  3Comments

tobrun picture tobrun  路  3Comments

ChrisLoer picture ChrisLoer  路  3Comments

friedbunny picture friedbunny  路  3Comments