2016-09-02 5 views
0

問題があります。
私はmapViewを持っており、mapViewには多くの注釈があります。
注釈を選択したindexPathを知るには
私は関数didSelectAnnotationViewを見つけましたが、私はそれをどのように使用するのか分かりません。mapviewでannotation indexpathを取得する方法

これは私のコードです:

BOOL firstLocationReceived; 

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)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<MKAnnotation>)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; 
} 

ありがとう!

+0

このhttp://stackoverflow.com/questions/12369328/detecting-the-selected-map-annotation-xcode –

+0

http://stackoverflow.com/questions/38156800/how-to-get-を見ます注釈インデックス内のマップキット/ 38157106#38157106これを参照してください –

答えて

0
  • あなたが

    -(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); 
    } 
    
  • は、選択した注釈点のannotation titleを取得し、すべての名前の私達の配列で、それのインデックスを見つけることができます、この方法によって、あなたの選択した注釈のインデックスを取得することができます。

+0

@imbeginner_sorryあなたはそれを確認しました..? – Gokul

関連する問題