0

我将一个完美运行的 6.1 应用程序升级到 Xcode 5/ios 7,它使用 MKMapKit 在地图视图上显示一些图钉。

现在注释引脚不显示。我确保我的 mapview 委托是正确的,并且正在调用 viewForAnnotation。我也确定它的标题和副标题已设置,但现在 pin 只是不显示。任何人都知道除此之外要寻找什么?

当它运行时,我看到我的纬度/经度、图钉标题和副标题已填写。

这是我的代码:

- (MKAnnotationView *) mapView:(MKMapView *) theMapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MKUserLocation class]])
    {
        return nil;
    }

    [self.mapView removeAnnotations:self.mapView.annotations];

    MKPointAnnotation *pt = (MKPointAnnotation *)annotation;

    NSString *PINNAME = pt.title;
    CLLocationDegrees lat = pt.coordinate.latitude;
    CLLocationDegrees lon = pt.coordinate.longitude;
    [Log log:TINFO :@"lat/lon %f - %f", lat, lon];
    [Log log:TINFO :@"pin title userid = %@", pt.title];
    [Log log:TINFO :@"subtitle: %@", pt.subtitle];

    MKPinAnnotationView *pinView = (MKPinAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier: PINNAME];

    if(pinView == nil)
    {
        pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:PINNAME];
        pinView.pinColor = MKPinAnnotationColorGreen;
    }
    else
    {
        pinView.annotation = annotation;
    }

    return pinView;
}
4

2 回答 2

3
- (MKAnnotationView *)mapView:(MKMapView *)mv viewForAnnotation:(id <MKAnnotation>)annotation
{
    //UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"ttile" message:@"working" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
    //[alert show];
    MKAnnotationView *view = nil;
    if(annotation != mv.userLocation) {
        // if it's NOT the user's current location pin, create the annotation
        Park *parkAnnotation = (Park*)annotation;
        // Look for an existing view to reuse
        view = [mv dequeueReusableAnnotationViewWithIdentifier:@"parkAnnotation"];
        // If an existing view is not found, create a new one
        if(view == nil) {
            view = [[MKPinAnnotationView alloc] initWithAnnotation:(id) parkAnnotation
                                                   reuseIdentifier:@"parkAnnotation"];
        }

        // Now we have a view for the annotation, so let's set some properties
        [(MKPinAnnotationView *)view setPinColor:MKPinAnnotationColorRed];
        [(MKPinAnnotationView *)view setAnimatesDrop:YES];
        [view setCanShowCallout:YES];

        // Now create buttons for the annotation view
        // The 'tag' properties are set so that we can identify which button was tapped later
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
        leftButton.tag = 0;
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        rightButton.tag = 1;

        // Add buttons to annotation view
        [view setLeftCalloutAccessoryView:leftButton];
        [view setRightCalloutAccessoryView:rightButton];
    }

    // send this annotation view back to MKMapView so it can add it to the pin
    return view;
}
于 2014-12-09T02:52:01.367 回答
3

您应该删除对removeAnnotationsfrom的调用viewForAnnotation,因为将其删除将删除您可能在地图上显示过程中的所有注释。如果您的意图是在添加新注释之前删除旧注释,您应该在调用之前执行此操作addAnnotation,而不是在viewForAnnotation.

与您当前的问题无关:

  1. 即使您真的想删除所有注释,如果您可能(在某些时候)MKUserLocation在地图上有 ,我建议只删除注释类不是的注释MKUserLocation。当我不小心MKUserLocationannotations.

  2. 顺便说一句,我可能会建议使用theMapView参数,而不是self.mapView. 您不想冒因一团糟而引发的问题的风险IBOutlet。无需引用作为参数传递的self.mapView时间。theMapView

  3. 您可以想象identifier为每个MKAnnotationView. 我在这段代码中没有看到任何需要这样做的地方,并且您会失去使用出列注释所获得的大部分效率。对特定类型的所有注释视图使用相同的identifier字符串(并且您似乎只有一种类型的注释)。

于 2013-10-05T06:07:31.323 回答