【问题标题】:ios 7 pin viewforannotation not showing upios 7 pin viewforannotation没有出现
【发布时间】:2013-10-12 05:19:40
【问题描述】:

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

现在注释图钉不显示了。我确定 我的 mapview 委托是正确的,并且正在调用 viewForAnnotation。 我也确定它的标题和副标题已设置,但现在别针只是没有 展示。任何人都知道除此之外还需要寻找什么?

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

这是我的代码:

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

【问题讨论】:

    标签: ios mkmapview pins


    【解决方案1】:

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

    与您当前的问题无关:

    1. 即使您真的想删除所有注释,如果您可能(在某些时候)在您的地图上有MKUserLocation,我建议您只删除注释类不是MKUserLocation。当我不小心从 annotations 的数组中删除了 MKUserLocation 时,我注意到了奇怪的行为。

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

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

    【讨论】:

    • 感谢罗伯的指点。好东西。我认为在这种情况下我误解了唯一标识符。如果您使用让我们说“thePin”作为标识符,并且放置了许多,那么“viewForAnnotation”如何知道要出列哪个引脚?我也知道 tableview 的工作方式,所以现在我很好奇。
    • @Tim 它会将所有已滚动到视野之外的引脚出列。它类似于UITableViewCell 的单元格标识符。所有外观相似的注释的一个标识符。
    • 看来我在一个不好的地方有一个 removeAnnotations 直到现在才咬我。我还发现当我调用 [self.mapView removeAnnotations:self.mapView.annotations];然后添加新的注解,两组注解还在地图上。很奇怪。
    • 在网上搜索了一段时间后,我发现要真正从地图中删除注释,您(显然)需要给 mapView.annotations(在这种情况下)一个指向引脚数组的新指针(删除旧的可能只是零)。不明显。希望它可以帮助别人。
    【解决方案2】:
    - (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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-23
      • 2013-03-07
      • 1970-01-01
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 2012-09-15
      相关资源
      最近更新 更多