Search code examples
iosplist

how to get specific data from plist when an annotation is tap and show it to label


Hello I have a mapview that get data from plist and is shown thru map annotation. What I wanted to do is when I tapped an annotation - its details will be shown in the labels. Will anyone teach me how to do it. Here's is my plist:

<array>
     <dict>
         <key>ID</key>
         <integer>1</integer>
         <key>title</key>
         <string>Toyota Shaw</string>
         <key>address</key>
         <string>304 Shaw Blvd., Mandaluyong City</string>
         <key>tel</key>
         <string>(02) 532 8428 </string>
         <key>latitude</key>
         <real>14.589393</real>
         <key>longitude</key>
         <real>121.039589</real>
         <key>img</key>
         <string>locator_gmap_marker.png</string>
    </dict>
         ....
    <dict>
        <key>ID</key>
        <integer>10</integer>
        <key>title</key>
        <string>Toyota Gen. Santos</string>
        <key>address</key>
        <string>National Hwy., City Heights, General Santos City, South Cotabato</string>
        <key>tel</key>
        <string>(083) 554 2994</string>
        <key>latitude</key>
        <real>6.119086</real>
        <key>longitude</key>
        <real>125.159881</real>
       <key>img</key>
       <string>gmap10.png</string>
   </dict>

Thanks in advance. Here's my code in the annotation:

 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{

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

// multiple annotation
if ([annotation isKindOfClass:[AddAnnotation class]])
{
    static NSString *reuseId = @"AddAnnotation";
    mapAnnot = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];

    if (mapAnnot == nil)
        mapAnnot = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId];
    else
        mapAnnot.annotation = annotation;


    AddAnnotation *ann = (AddAnnotation *)annotation;

    mapAnnot.canShowCallout = YES;
    mapAnnot.image = [UIImage imageNamed:ann.image];

    return mapAnnot;
}
return nil;

} // for user location - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

// add annotation to user location -
addAnnotation = [[AddAnnotation alloc]init];
addAnnotation.coordinate = userLocation.coordinate;
addAnnotation.title = @"I'm here";
addAnnotation.image = @"me";
[myArrayLoc addObject:addAnnotation];

[self.dealerMapLocation addAnnotations:myArrayLoc];

}


Solution

  • I guess that AddAnnotation class is not standard, so I would be better if you could show its declaration for checking what you changed. Anyway, I think that adding a detail button to your annotation, and then linking an action to this button will solve your problem

     UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
    [button addTarget:self action:@selector(doStuffMethod) forControlEvents:UIControlEventTouchUpInside]; 
    addAnnotation.rightCalloutAccessoryView = button;