Obj-C-来自XIB的自定义地图注释标注视图

时间:2018-09-24 00:11:40

标签: ios objective-c mapkit

当我点击MapView上的注释时,我试图显示自定义视图/ XIB。就是说,我迅速找到了该问题的各种答案-但对于目标C却找不到。

当前,我可以使用以下代码显示自定义注释:

ViewController.m

-(void)viewDidLoad {


    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[@"node_title"];
            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 *)mapView viewForAnnotation:(id <MKAnnotation>)annotation{



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

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

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

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

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

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

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

                return pinView;
            }
            return nil;

        }

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


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

    }

    -(void)calloutTappedTwo:(UITapGestureRecognizer *) sender
    {

        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];

        }

    }

也就是说,如果我希望创建的自定义XIB在点击每个批注时显示为标注,我应该在哪里/怎么称呼它?

0 个答案:

没有答案
相关问题