25

I am new to Maps Concept.

Please find the attached image once.

enter image description here

when i click the "pin" i am getting message says "Admire your smile" or any text..... Now i want like... when we select the table view, i need to raise that message for that pin(with out user interaction for that pin)...

I am using below code for This maps and pins.

    -(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
customLocation.latitude=[casesobjc.latitude_string doubleValue];
        customLocation.longitude=[casesobjc.longitude_string doubleValue];
 MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:casesobjc.locationForMap_string andCoordinate:customLocation];
[mapView addAnnotation:newAnnotation];
}
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
 MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: @"annotation_ID"];
if (pin == nil) {
        pin = [[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"annotation_ID"];
    } else {
        pin.annotation = annotation;
    }
    pin.pinColor = MKPinAnnotationColorPurple;
    pin.animatesDrop = YES;
    pin.canShowCallout=YES;
return pin;
}

Now i will show all the pins once like below.

enter image description here

How to do When we click on table view i need to show the title for that pin like how we show when we select a pin?

Thanks in Advance

Craig
  • 8,093
  • 8
  • 42
  • 74
Babul
  • 1,268
  • 2
  • 15
  • 42
  • did u mention the anotation tap view with custom image / custom map pin call out view? – Arun Apr 02 '13 at 07:21
  • i did not understand?... i used this code but not works.. i.e., customLocation.latitude=appdelegate.appDelegateLatitude; customLocation.longitude=appdelegate.appDelegateLongitude; MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:casesobjc.locationForMap_string andCoordinate:customLocation]; [mapView selectAnnotation:newAnnotation animated:YES]; @Spynet – Babul Apr 02 '13 at 07:22
  • Just pass the title name when the row is selected and set the title for annotation – Sugan S Apr 02 '13 at 07:27
  • i set the titles.. which method i need to call. i set the titles for all pins like this MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:casesobjc.locationForMap_string andCoordinate:customLocation]; @sugan.s – Babul Apr 02 '13 at 07:29
  • what is casesobjec.location and what u get in tableviewDidSelectedRowAtIndexPath @Babul – Sugan S Apr 02 '13 at 07:32
  • I am loading static data (different location names with respective latitude and longitude)... when i select the table view i get an array with objects that is caseObject. It contains the title, latitude, longitude . @sugan.s – Babul Apr 02 '13 at 07:37
  • by using [mapView selectAnnotation:newAnnotation animated:YES]; this one you can't get your requirement? – Balu Apr 02 '13 at 08:56
  • ok....Thank you.....how can i achieve my req.? @Sunny – Babul Apr 02 '13 at 09:03
  • try like this [mapView selectAnnotation:[[mapView annotations] objectAtIndex:1] animated:YES]; may be it'l help you. – Balu Apr 02 '13 at 09:05
  • i write code like this .. customLocation.latitude=appdelegate.appDelegateLatitude; customLocation.longitude=appdelegate.appDelegateLongitude; MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:casesobjc.locationForMap_string andCoordinate:customLocation]; [mapView selectAnnotation:newAnnotation animated:YES]; @Sunny – Babul Apr 02 '13 at 09:07
  • Take a look at http://stackoverflow.com/questions/978897/how-to-trigger-mkannotationviews-callout-view-without-touching-the-pin – Krish Allamraju Apr 02 '13 at 09:16
  • Hi @Babul Have you found a solution to this issue yet? I think I have quite [a similar issue](http://stackoverflow.com/questions/28144466/how-to-select-map-pin-when-tapping-table-row-that-associates-with-it). Could you please share the solution with me? Thanks. – SanitLee Jan 29 '15 at 05:45

2 Answers2

32

The method that you are looking for is selectAnnotation:.

In your didSelectRowAtIndexPath, you have to find the annotation associated with the table row.

Once you find it, you can tell it, to select it by using:

[mapView selectAnnotation:foundAnnotation animated:YES];
adamweeks
  • 1,332
  • 2
  • 14
  • 21
  • I think I have quite a similar issue. Could you please help take a look into my [question](http://stackoverflow.com/questions/28144466/how-to-select-map-pin-when-tapping-table-row-that-associates-with-it) also? I just cannot figure it out especially on how to find the annotation associated with the table row. – SanitLee Jan 27 '15 at 14:16
6

The same as the Objective C answer, in your tableview you have a list of annotations. In Swift in the didSelectRow:

For Swift 4:

// annotations would be the array of annotations - change to your required data sets name
let selectedAnnotation = annotations[indexPath.row]
self.mapView.selectAnnotation(selectedAnnotation, animated: true)

Short and simple.

App Dev Guy
  • 5,396
  • 4
  • 31
  • 54