默认情况下,地图中不显示callout

时间:2013-07-01 09:57:10

标签: ios map annotations callout

我试图在没有点击引脚的情况下在引脚上显示标注。简而言之,我想在引脚放到地图上时显示标注。

这是我正在使用的代码:

-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation         //  Method to handle all the annotations on map.
{

    if([annotation isKindOfClass: [MKUserLocation class]])
        return nil;

    static NSString *annotationID = @"MyAnnotation";

    MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID] ;

    if (!pinView) {
        pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];
        pinView.canShowCallout = YES;
        pinView.image = [UIImage imageNamed:@"map_pin.png"];

        //Setting Right call button
        pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

        //Setting Left Call button
        self.favButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.favButton.frame = CGRectMake(0, 0, 23, 23);
        self.favButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        self.favButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.favButton setImage:[UIImage imageNamed:@"favourite.png"] forState:UIControlStateNormal];
        pinView.leftCalloutAccessoryView = self.favButton;

        [self.mapView selectAnnotation:annotation animated:YES];
    }
    return pinView;

}

任何帮助都会很明显。

1 个答案:

答案 0 :(得分:0)

试试这个:

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
    for (id<MKAnnotation> currentAnnotation in mapView.annotations) {
        if ([currentAnnotation isEqual:annotationToSelect]) {
            [mapView selectAnnotation:currentAnnotation animated:YES];
        }
    }
}

编辑: - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation //处理地图上所有注释的方法。 {

if([annotation isKindOfClass: [MKUserLocation class]])
    return nil;

static NSString *annotationID = @"MyAnnotation";

MKPinAnnotationView *pinView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationID] ;

if (!pinView) {
    pinView = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annotationID];
    pinView.canShowCallout = YES;
    pinView.image = [UIImage imageNamed:@"map_pin.png"];

    //Setting Right call button
    pinView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    //Setting Left Call button
    self.favButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.favButton.frame = CGRectMake(0, 0, 23, 23);
    self.favButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.favButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [self.favButton setImage:[UIImage imageNamed:@"favourite.png"] forState:UIControlStateNormal];
    pinView.leftCalloutAccessoryView = self.favButton;

            **[self performSelector:@selector(selectAnnotation:) withObject:annotation afterDelay:0.5];**

}
return pinView;

}

- (void)selectAnnotation:(id < MKAnnotation >)annotation
{
    [self.mapView selectAnnotation:annotation animated:NO];
}