单击注释时如何访问可用数据?

时间:2016-03-07 12:22:46

标签: ios objective-c mapkit xcode7 mkannotation

我有几个注释点。我想要做的是当我点击我要显示该数据的注释点时。我有一个带标题和NSDictionary的自定义Annotation类。字典包含图像URL和ID。我想在点击特定注释时获取这些内容。

编辑:我知道didSelectAnnotation,但我如何访问数据?

-(void)annotations{
    CLLocationCoordinate2D pinPoint;
    for (NSDictionary * dict in _dataArray) {
        pinPoint.latitude =[[dict valueForKey:@"lat"] doubleValue];
        pinPoint.longitude = [[dict valueForKey:@"lng"] doubleValue];
        _myAnnotation = [[MapViewAnnotation alloc]initWithTitle:[dict valueForKey:@"name"]andCoordinate:pinPoint andData:dict];

        MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:pinPoint addressDictionary:nil] ;
        _destination = [[MKMapItem alloc] initWithPlacemark:placemark];
        _myAnnotation.title=[dict valueForKey:@"name"];

        [self.mapKit addAnnotation:_myAnnotation];
    }

}

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

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




}

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(MapViewAnnotation*)annotation
{
    // If it's the user location, just return nil.
    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    // Handle any custom annotations.
    if ([annotation isKindOfClass:[MapViewAnnotation class]])
    {
        // Try to dequeue an existing pin view first.
        MKAnnotationView *pinView = (MKAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomPinAnnotationView"];
        if (!pinView)
        {
            // If an existing pin view was not available, create one.
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"CustomPinAnnotationView"];
            pinView.canShowCallout = YES;
            NSLog(@"%@",[annotation.data valueForKey:@"name"]);
            pinView.image = [UIImage imageNamed:@"image"];
            pinView.calloutOffset = CGPointMake(0, 32);
            UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
            pinView.rightCalloutAccessoryView = rightButton;
        } else {
            pinView.annotation = annotation;
        }
        return pinView;
    }
    return nil;
}


-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    [request setTransportType:MKDirectionsTransportTypeWalking];
    request.source = [MKMapItem mapItemForCurrentLocation];
    request.destination = _destination;
    request.requestsAlternateRoutes = NO;
    MKDirections *directions =
    [[MKDirections alloc] initWithRequest:request];

    [directions calculateDirectionsWithCompletionHandler:
     ^(MKDirectionsResponse *response, NSError *error) {
         if (error) {
             // Handle Error
         } else {
             [self showRoute:response];
         }
     }];
}

编辑第二名:

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{    
    MapViewAnnotation * annotation = (MapViewAnnotation *) view;
    NSLog(@"%@",[annotation.data valueForKey:@"name"]);
    NSURL *url =[NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:@"SPOTTD_profilePic"]];
    NSData *imageData = [NSData dataWithContentsOfURL:url];
    _imgMyProfile.image = [UIImage imageWithData:imageData];

}

3 个答案:

答案 0 :(得分:1)

您可以将didSelectAnnotationView方法与MKAnnotationView的{​​{3}}属性结合使用:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
     if ([view.annotation isKindOfClass:[MapViewAnnotation class]]) {
          MapViewAnnotation *annotation = (MapViewAnnotation)view.annotation;
     }
}

在您将注释安全地投射到自定义类型后,您可以访问其属性,例如NSDictionary

答案 1 :(得分:0)

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
 NSString *latPoint2 = [NSString stringWithFormat:@"%.6f", view.annotation.coordinate.latitude];
 NSString *longPoint2 =[NSString stringWithFormat:@"%.6f", view.annotation.coordinate.longitude];

答案 2 :(得分:0)

还有一种注释方法  - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {}。在此方法中,您可以在点击注释时显示数据。希望这对您有所帮助。如果有任何疑问让我知道。谢谢