2

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

}
Thejus
  • 169
  • 12

3 Answers3

1

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.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
  • im getting an error [MKAnnotationView data]: unrecognised selector sent to instance 0x7fae1d5f1020 here data is my NSDictionary in Custom Annotation Class – Thejus Mar 07 '16 at 12:35
  • Is `data` a property of `MapViewAnnotation`? If so, try `myAnnotation.data`. – Glorfindel Mar 07 '16 at 12:39
  • please check my 2nd edit. Im doing the same. the exception is being thrown at NSLog(@"%@",[annotation.data valueForKey:@"name"]); – Thejus Mar 07 '16 at 12:41
  • Please note that the `view` is an `MKAnnotationView`, you can't cast it to a `MapViewAnnotation`. You'll need its `annotation` property. You can copy-paste the four lines (starting with `NSLog`) in my code, then it should work. – Glorfindel Mar 07 '16 at 12:49
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];
Sabby
  • 403
  • 3
  • 15
0

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