in Education by
I have a problem. I have a mapView, and I have many annotations in my mapView. how to know the indexPath that I select the annotation. I find the function didSelectAnnotationView but I don't know how to use it. This is my code: BOOL firstLocationReceived; -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { CLLocation *currentLocation = locations.lastObject; //current location if(firstLocationReceived == NO) { MKCoordinateRegion region = _shopMapView.region; region.center = currentLocation.coordinate; region.span.latitudeDelta = 0.05; region.span.longitudeDelta = 0.05; [_restaurantMapView setRegion:region animated:true]; //Add Annotation for (int i = 0; i < self.items.count ; i++) { MKPointAnnotation *annotation = [MKPointAnnotation new]; NSDictionary *item = self.items[i]; CLLocationDegrees latitude = [item[@"latitude"] doubleValue]; CLLocationDegrees longitude = [item[@"longitude"] doubleValue]; annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude); annotation.title = item[@"name"]; annotation.subtitle = item[@"address"]; [_shopMapView addAnnotation:annotation]; firstLocationReceived == YES; } } } -(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id)annotation { if (annotation == mapView.userLocation) { return nil; } NSString *identifier = @"shop"; MKAnnotationView *result = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier]; if(result ==nil) { result = [[MKAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:identifier]; } else { result.annotation = annotation; } result.canShowCallout = true; UIImage *annotationViewImage = [UIImage imageNamed:@"point.png"]; result.image = annotationViewImage; result.leftCalloutAccessoryView = [[UIImageView alloc]initWithImage:annotationViewImage]; UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; result.rightCalloutAccessoryView = button; return result; } Thanks! JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You can get your selected annotation index by this way, -(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{ MKPointAnnotation * annotation = [[MKPointAnnotation alloc]init]; annotation = view.annotation; NSString * selectedTitle = [NSString stringWithFormat:@"%@",annotation.title]; NSInteger index = [[self.items valueForKey:@"name"] indexOfObject:selectedTitle]; //Considering self.items as NSDictionary consist of all annotation names. NSLog(@"You selected index: %ld",(long)index); } Swift func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { var annotation = MKPointAnnotation() if let anAnnotation = view.annotation as? MKPointAnnotation { annotation = anAnnotation } let selectedTitle = "\(annotation.title ?? "")" let index: Int? = (items["name"] as? NSArray)?.index(of: selectedTitle) //Considering self.items as NSDictionary consist of all annotation names. print("You selected index: \(Int(index))") } Get the annotation title of selected annotation point and lets find it's index in our array of all names.

Related questions

0 votes
    I'm using flutter_map package and I need to draw path between 2 marker. I know map_view package provided ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
0 votes
    @RequestMapping annotation is used to map a HTTP request method (GET or POST) to a specific class or method in the ... which will handle the respective request. A. True B. False...
asked Nov 8, 2022 in Education by JackTerrance
0 votes
    Annotation used for handling GET requests? 1. @PostMapping 2. @GetMapping 3. @PutMapping 4. None of the Above...
asked Jul 8, 2021 in Education by JackTerrance
0 votes
    The annotation used to import additional configuration classes is ___________. A. @Import B. @Include C. @EnableImport D. Any of the options...
asked Nov 8, 2022 in Education by JackTerrance
0 votes
    I don't need so many pins , they are crowding the area. I used the output of kml parser to draw ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    I have the below query to fetch specific columns of a entity/table from database @Query("SELECT u.email, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    new to play and just trying to follow the video on: http://www.playframework.org/ I'm coming so ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    Which of the below is an incorrect annotation with respect to JUnits? (a) @Test (b) @BeforeClass (c) @Junit ... & Miscellaneous of Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Which version of Java introduced annotation? (a) Java 5 (b) Java 6 (c) Java 7 (d) Java 8 This ... Lifecycle & Annotations of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Annotation type definition looks similar to which of the following? (a) Method (b) Class (c) Interface (d) ... & Annotations of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Which of the following is not pre defined annotation in Java? (a) @Deprecated (b) @Overriden (c) @SafeVarags ... & Annotations of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Which one of the following is not ID generating strategy using @GeneratedValue annotation? (a) Auto (b) Manual ... Annotations of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Which one of the following is not an annotation used by Junit with Junit4? (a) @Test (b) @BeforeClass (c) ... & Annotations of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Using which annotation non visible or private method can be tested? (a) @VisibleForTesting (b) @NonVisibleForTesting (c ... of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    Which of the following annotation is used to avoid execution of Junits? (a) @NoTest (b) @explicit (c) @avoid ... & Annotations of Java Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
...