iOS mapkit: MKAnnotationView not centered inside MKPolygon

420 Views Asked by At

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.

0

There are 0 best solutions below