我有几个MKPolygons
我使用rendererForOverlay
方法覆盖。现在在每个这样的多边形内部,我需要我的注释视图来显示。
我观察到注释视图位置非常不准确。只有当我完全缩放到MKPolygon时,才会看到注释视图位于MKPolygon内部。
我尝试设置MKAnnotationView
派生类实例的中心,但无济于事。
我尝试使用centerOffset属性,但无济于事。我不确定我可以传递给它的是什么值,以使整个事物在MKPolygon中居中。
我正在使用我自己的MKAnnotationView
派生子类,不是在iOS MKPinAnnotationView
中构建的。这是我的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
子类,我已经覆盖了centerOffset
setter。使用iOS模拟器8.0版进行测试。