0

我有几个MKPolygons我正在使用rendererForOverlay方法覆盖。现在在每个这样的多边形内,我需要显示我的注释视图。

我观察到注释视图位置非常不准确。只有当我完全放大到 MKPolygon 时,我才能看到以 MKPolygon 为中心的注释视图。

我尝试设置MKAnnotationView派生类实例的中心,但无济于事。

我尝试使用 centerOffset 属性,但无济于事。我不确定我可以传递给它什么值以使整个事物以 MKPolygon 为中心。

我正在使用我自己的MKAnnotationView派生子类,而不是在 iOSMKPinAnnotationView等中构建。这是我的 viewforAnnotation 实现:

-(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;
}

更新:

事实证明,设置 centerOffset 没有效果。尝试将其设置在内部的不同位置,viewForAnnotation但无济于事。对于任何想知道的人,我有自己的MKAnnotationView子类,我在其中覆盖了centerOffsetsetter。使用 iOS 模拟器 8.0 版进行测试。

4

0 回答 0