iOS / Obj-C:两个MKMapViews,一个MapView的注释没有执行?

时间:2017-07-07 16:33:30

标签: ios objective-c

我在一个ViewController中有两个单独的mapview。每个地图视图都会显示一组不同的注释 - 一个地图显示陌生人位置,另一个地图显示朋友位置。当我在第一个mapview(mapView)上点击注释时,我会得到一个包含正确用户数据的详细信息视图。但是,当我在第二个地图视图(friendsMapView)上点击注释时,数据将从填充第一个mapView的字典中提取。记录错误后,我发现对于friendsMapView的didSelectAnnotation永远不会被执行?我怎样才能解决这个问题?

代码未执行(编辑):

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

    if (self.mapView == self.mapView) {
        // Do something specific to self.mapView

        PointAnnotation *selectedPoint = (PointAnnotation *) view.annotation;

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

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

        NSMutableDictionary *dictionary = self.addressData[selectedPoint.index];
        yourViewController.mapuserData = dictionary;

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


    } else if (self.mapView == self.friendsMapView) {
        // Do something specific to self.friendsMapView

        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.mapuserData = dictionary;

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

    }



    }

完整代码: MapViewController.m

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{

    if (self.mapView == self.mapView) {

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1300, 1300);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

    } else if (self.mapView == self.friendsMapView) {

        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1300, 1300);
        [self.friendsMapView setRegion:[self.friendsMapView regionThatFits:region] animated:YES];

    }

}

    - (NSString *)deviceLocation {
        return [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
    }
    - (NSString *)deviceLat {
        return [NSString stringWithFormat:@"%f", self.locationManager.location.coordinate.latitude];
    }
    - (NSString *)deviceLon {
        return [NSString stringWithFormat:@"%f", self.locationManager.location.coordinate.longitude];
    }
    - (NSString *)deviceAlt {
        return [NSString stringWithFormat:@"%f", self.locationManager.location.altitude];
    }

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

            self.addressData = [responseObject mutableCopy];


            NSLog(@"%@", responseObject);


            int index = 0;   //Index to track the data source index while select the annotation call out view.

            for (NSMutableDictionary *multiplelocations in self.addressData) {

                NSString *location = multiplelocations[@"street_address"];
                NSLog(@"Pull addresses %@", location);
                NSString *userNames = multiplelocations[@"users_name"];
                NSString *userBio = multiplelocations[@"userbio"];


                CLGeocoder *geocoder = [[CLGeocoder alloc] init];

                [geocoder 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.mapView.region;

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


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

                                     [self.mapView addAnnotation:point];
                                 }
                             }
                 ];
                index = index + 1;
            }

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





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

            self.friendData = [responseObject mutableCopy];


            NSLog(@"THIS IS FRIEND DATA %@", self.friendData);

            int index = 0;

            for (NSMutableDictionary *multiplelocationsFriend in self.friendData) {


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


                       NSLog(@"LOCATION IS HERE %@", location);



                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 /= 8.0;
                                     region.span.latitudeDelta /= 8.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 {

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

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

                static NSString* AnnotationIdentifier = @"AnnotationIdentifier";

                MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                                 reuseIdentifier:AnnotationIdentifier];
                annotationView.canShowCallout = YES;
                annotationView.image = [UIImage imageNamed:@"mappaw.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 {

            PointAnnotation *selectedPoint = (PointAnnotation *) view.annotation;

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

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

            NSMutableDictionary *dictionary = self.addressData[selectedPoint.index];
            yourViewController.mapuserData = dictionary;

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

        }


    -(MKAnnotationView*)friendsMapView:(MKMapView*)friendsMapView viewForAnnotation:(id<MKAnnotation>)annotation {
        // If you are showing the users location on the map you don't want to change it
        MKAnnotationView *view2 = nil;
        if (annotation != self.friendsMapView.userLocation) {

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

                static NSString *AnnotationIdentifier = @"FriendAnnotationIdentifier";

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

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


            }
        }
        return view2;
    }

    - (void)friendsMapView:(MKMapView *)friendsMapView didSelectAnnotationView:(MKAnnotationView *)view2
    {

        PointAnnotation *selectedPoint = (PointAnnotation *) view2.annotation;

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

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

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

        yourViewController.mapuserData = dictionary;

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

        NSLog(@"Selected!");


    }

1 个答案:

答案 0 :(得分:1)

您无法为委托方法制作方法签名。

以下是正确的:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation

以下是一些永远不会被视为委托方法的组合方法:

- (void)friendsMapView:(MKMapView *)friendsMapView didUpdateUserLocation:(MKUserLocation *)userLocation

只需实现正确的委托方法即可。两个地图视图都将使用相同的委托方法。您可以使用mapView参数进行相应操作。

例如,您的两个didUpdateUserLocation委托方法如下所示:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 1300, 1300);
    [mapView setRegion:[mapView regionThatFits:region] animated:YES];
}

一个委托方法适用于两个地图视图。

如果您确实需要根据地图视图执行不同的操作,请执行以下操作:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    if mapView == self.mapView {
        // Do something specific to self.mapView
    } else if mapView == self.friendsMapView {
        // Do something specific to self.friendsMapView
    }
}