0

I have several MKPolygons for which I am using rendererForOverlay method override. Now inside each such polygons I need my annotation view to display.

I observe that the annotation view positions are quite inaccurate. Only when I zoom fully to the MKPolygon do I see the annotation view centered inside that MKPolygon.

I tried setting center of the MKAnnotationView derived class instance, but to no avail.

I tried playing with centerOffset property but to no avail. I am not sure what values I could pass to it to make the whole thing centered inside MKPolygon.

I am using my own MKAnnotationView derived subclass, not built in iOS MKPinAnnotationView or such.. Here is my viewforAnnotation implementation:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        //show default blue dot for user location...
        return nil;
    }

    static NSString *reuseId = @"MapAnnotationID";

    MWAnnotationView *av = (MWAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];

    if (av == nil)
    {
        av = [[MWAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
        av.canShowCallout = NO;

        //note that this my custom UIView derived class to show UI.
        MWMapAnnotationView * mapAnnView = [[[NSBundle mainBundle] loadNibNamed:@"MapAnnotationView" owner:nil options:nil] firstObject];

        mapAnnView.tag = TAG_ANNOTATION;

        [av addSubview:mapAnnView];
        av.centerOffset = CGPointMake(0, -15);
    }
    else
    {
        av.annotation = annotation;
    }

    if (map.region.span.longitudeDelta > MAX_LONGITUDEDELTA / 4)
    {
        av.hidden = YES;
    }
    else
    {
        av.hidden = NO;
    }

    //custom UI elements of my subview
    mapAnnView.labelCapital.text = capital;
    mapAnnView.labelPopulation.text = population;

    if (imageName)
    {
        mapAnnView.imageAnnotation.image = [UIImage imageNamed:imageName];
    }
    else
    {
        mapAnnView.imageAnnotation.image = nil;
    }    

    return av;
}

UPDATE:

It turns out setting centerOffset has no effect. Tried setting it at different points inside viewForAnnotation but to no avail. For anyone who is wondering, I have my own MKAnnotationView subclass where I have overridden centerOffset setter. Tested with iOS simulator version 8.0.

Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
  • Some screenshots might help but remember that centerOffset is not scaled automatically with the zoom level. It is a fixed value in screen CGPoints. This can cause the annotation to appear at the wrong position depending on the zoom. –  Dec 16 '14 at 22:02
  • I can't have any screenshots because of NDA - but to summarize - initially annotation appear at bottom right of the polygon. Zooming to the MAX POSSIBLE value brings it to the polygon center. So what you mean is that there is no way to modify CGPoint to match that of polygon center? – Nirav Bhatt Dec 17 '14 at 06:00
  • Imagine you used the default red pin annotation and put it at say London. When zoomed in very close, the bottom tip of the pin will be at London center and the top of the pin might be a few streets away. When zoomed out very far, the bottom tip of the pin will _still_ be at London center (because its centerOffset is set based accurately on the pin image) but the _top_ of the pin might be miles away like in Scotland. –  Dec 17 '14 at 11:44
  • If you use different images for each annotation, the center offset might need to be different for each based on the "center" of the image. –  Dec 17 '14 at 11:45
  • So basically my centeroffset should be like (0,-SOME_VALUE) as some posts suggest? It never convinced me. I think it should be (0, SOME_VALUE) because the coordinate is down (London) while the center of the annotation view is up there (Scotland). Nevertheless, I will try a few combinations now. – Nirav Bhatt Dec 17 '14 at 13:46
  • My problem is that I don't add annotations for specific coordinates, but for specific areas - MKPolygons. So I assume that those annotations are added for center coordinates of those polygons. Probably, that is screwing things up. – Nirav Bhatt Dec 17 '14 at 13:56

0 Answers0