Obj-C - MKMapView注释标注没有显示?

问题描述 投票:-1回答:2

我的mapview(self.friendsMapView)上有注释,在点击时应该显示一个标注。每个注释显示标题和描述(正文)。出于某种原因,无论我尝试什么,如果我的注释被点击,我的标注将不会显示。我在这里错过了什么吗?

一点背景:我以前在一个ViewController中有两个mapView(并且,我的标注工作正常)。但是在我决定删除一个mapview(self.neighboursMapView)及其代码之后,我对self.friendsMapView的标注似乎已停止工作了。

ViewController.m

    - (void)viewDidLoad {
        [super viewDidLoad];


         friendsMapView.showsUserLocation = YES;
        [friendsMapView setMapType:MKMapTypeStandard];
        [friendsMapView setZoomEnabled:YES];
        [friendsMapView setScrollEnabled:YES];

        NSMutableDictionary *viewParamsFriend = [NSMutableDictionary new];
        [viewParamsFriend setValue:@"accepted_friends" forKey:@"view_name"];
        [DIOSView viewGet:viewParamsFriend success:^(AFHTTPRequestOperation *operation, id responseObject) {


            self.friendData = [responseObject mutableCopy];

            int index = 0;

            for (NSMutableDictionary *multiplelocationsFriend in self.friendData) {


                NSString *location = multiplelocationsFriend[@"address2"];
                NSString *userNames = multiplelocationsFriend[@"first name"];
                NSString *userBio = multiplelocationsFriend[@"body"];


                CLGeocoder *geocoderFriend = [[CLGeocoder alloc] init];
                [geocoderFriend geocodeAddressString:location
                             completionHandler:^(NSArray* placemarks, NSError* error){
                                 if (placemarks && placemarks.count > 0) {
                                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                                     MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

                                     MKCoordinateRegion region = self.friendsMapView.region;

                                     region.span.longitudeDelta /= 150.0;
                                     region.span.latitudeDelta /= 150.0;


                                     PointAnnotation *point = [[PointAnnotation alloc] init];
                                     point.coordinate = placemark.coordinate;
                                     point.title = userNames;
                                     point.subtitle = userBio;
                                     point.index = index;  // Store index here.

                                     [self.friendsMapView addAnnotation:point];
                                 }
                             }
                 ];

                index = index + 1;

            }

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Failure: %@", [error localizedDescription]);
        }];


    }

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

    if([annotation isKindOfClass:[meetupAnn class]]) {
        static NSString *identifier = @"currentLocation";
        SVPulsingAnnotationView *pulsingView = (SVPulsingAnnotationView *)[self.friendsMapView dequeueReusableAnnotationViewWithIdentifier:identifier];

        if(pulsingView == nil) {
            pulsingView = [[SVPulsingAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
            pulsingView.annotationColor = [UIColor colorWithRed:0 green:0.678431 blue:0 alpha:1];
            pulsingView.canShowCallout = YES;
        }

        return pulsingView;
    }


    MKAnnotationView *view = nil;
    if (annotation != self.friendsMapView.userLocation) {

        NSLog(@"Friend Map Showing");

        view = [self.friendsMapView dequeueReusableAnnotationViewWithIdentifier:@"AnnotationIdentifier"];
        if (!view) {

            static NSString* AnnotationIdentifier = @"AnnotationIdentifier";

            MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                            reuseIdentifier:AnnotationIdentifier];
            annotationView.canShowCallout = YES;
            annotationView.image = [UIImage imageNamed:@"mapann3.png"];
            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

            annotationView.rightCalloutAccessoryView = rightButton;
            annotationView.canShowCallout = YES;
            annotationView.draggable = YES;


            return annotationView;



        }
    }

    return view;

}

    - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
    {

       if (mapView == self.friendsMapView) {

            UITapGestureRecognizer *tapGesture2 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(calloutTappedTwo:)];
            [view addGestureRecognizer:tapGesture2];

        }



    }


    -(void)calloutTappedTwo:(UITapGestureRecognizer *) sender
    {
        NSLog(@"CALL OUT TWO TAPPED");

        MKAnnotationView *view = (MKAnnotationView*)sender.view;

        id <MKAnnotation> annotation = [view annotation];
        if ([annotation isKindOfClass:[MKPointAnnotation class]])
        {
            PointAnnotation *selectedPoint = (PointAnnotation *) view.annotation;

            UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

            OtherUserViewController *yourViewController = (OtherUserViewController *)[storyboard instantiateViewControllerWithIdentifier:@"OtherUserViewController"];

            NSMutableDictionary *dictionary = self.friendData[selectedPoint.index];
            yourViewController.frienduserData = dictionary;

            [self.navigationController pushViewController:yourViewController animated:YES];

        }

    }
ios objective-c mkmapview
2个回答
0
投票

尝试使用以下配置的viewForAnnotation方法。

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


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

    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {

        MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
        if (!pinView)
        {

            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];

            pinView.canShowCallout = YES;
            pinView.image = [UIImage imageNamed:@"annotation"];
            pinView.calloutOffset = CGPointMake(0, 0);


            UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"annotation"]];
            pinView.leftCalloutAccessoryView = iconView;


        } else {
            pinView.annotation = annotation;
        }
        return pinView;
    }
    return nil;

}

0
投票

尝试使用以下配置的viewForAnnotation方法。

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


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

    if ([annotation isKindOfClass:[MKPointAnnotation class]])
    {

        MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
        if (!pinView)
        {

            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];

            pinView.image = [UIImage imageNamed:@"annotation"];

        } else {
            pinView.annotation = annotation;
        }
        pinView.canShowCallout = YES;
        pinView.calloutOffset = CGPointMake(0, 0);

        UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"annotation"]];
        pinView.leftCalloutAccessoryView = iconView;

        return pinView;
    }
    return nil;

}
© www.soinside.com 2019 - 2024. All rights reserved.