MKPointAnnotation不起作用

时间:2013-05-13 23:41:39

标签: objective-c cocoa-touch mkmapview mkannotation

您好我有这个方法:

-(void)adresseZeigen
{
    NSLog(@"%s",__PRETTY_FUNCTION__);

    selectedAnnotation = [[MKPointAnnotation alloc]init];

    selectedAnnotation.title = selectedCompanyName;
    selectedAnnotation.subtitle = selectedCompanyAdresse;
    selectedAnnotation.coordinate = selectedCompanyPoint;


    NSLog(@"selected title: %@",selectedAnnotation.title);
    NSLog(@"selected subtitle: %@",selectedAnnotation.subtitle);
    NSLog(@"selected latitude is: %f", self.selectedAnnotation.coordinate.latitude );
    NSLog(@"selected longitude is: %f", self.selectedAnnotation.coordinate.longitude );

    [mapView addAnnotation:selectedAnnotation];

    MKCoordinateRegion selectedRegion;

    selectedRegion.center = selectedCompanyPoint;
    selectedRegion.span.longitudeDelta = 0.01;
    selectedRegion.span.latitudeDelta = 0.01;
    [mapView setRegion:selectedRegion animated:YES];

}

它实际上应该在我的mapview中给我一个注释。

我的日志输出是:

-[SecondViewController adresseZeigen]
selected title: Company 2
selected subtitle: Company 2 Adresse
selected latitude is: 48.620000
selected longitude is: 9.460000

但不知怎的,我在地图上没有得到注释。

有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

仅此一项不会在地图上给你任何实际的注释视图。为了在地图中有注释视图,您需要为视图控制器声明MKMapViewDelegate,将其声明为mapview委托并实现该方法:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation

这是实际为地图视图生成注释视图的方法。在您的情况下,您可能希望使用MKPinAnnotationView作为MKPointAnnotation!

您可以查看协议参考here

编辑:该方法的一个示例实现(假设您的所有MKPointAnnotations具有相同的目的)可以是:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    static NSString *reuseId = @"MyReuseID";
    MKPinAnnotationView * view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
    if (!view) {
        view = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
        //custom setup of your view, such as
        //view.canShowCallout = YES;
    }
    return view;
}