Thejus
Thejus

Reputation: 228

How do I access the available data when I click an annotation?

I have several annotation points. What I want to do is when I click an annotation point I want to display that data. I have a custom Annotation class with a title and an NSDictionary. The dictionary contains an image URL and ID. I want to fetch these when I click on a specific annotation.

EDIT: I know about the didSelectAnnotation, but how do I access the data?

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

EDIT 2nd :

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

}

Upvotes: 2

Views: 163

Answers (3)

Glorfindel
Glorfindel

Reputation: 22651

You can use the didSelectAnnotationView method, in combination with the annotation property of the MKAnnotationView:

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

After you have safely cast the annotation to your custom type, you can access its properties like the NSDictionary.

Upvotes: 1

testing tcyonline
testing tcyonline

Reputation: 47

There is one more method for annotation which is - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {}.In this method you can show your data on click of annotation.Hope this will help you. If any query let me know. Thanks

Upvotes: 0

Sabby
Sabby

Reputation: 403

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

Upvotes: 0

Related Questions