【问题标题】:How to get annotation indexpath in mapview如何在mapview中获取注释索引路径
【发布时间】:2016-09-02 05:23:31
【问题描述】:

我有一个问题。
我有一个 mapView,我的 mapView 中有很多注释。
如何知道我选择注解的indexPath
找到了didSelectAnnotationView这个函数,但是不知道怎么用。

这是我的代码:

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

谢谢!

【问题讨论】:

标签: ios objective-c annotations mkmapview


【解决方案1】:
  • 您可以通过这种方式获取您选择的注释索引,

    -(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);
    }
    

    斯威夫特

    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))")
    }
    
  • 获取所选注释点的annotation title,并在我们的所有名称数组中找到它的索引。

【讨论】:

  • @imbeginner_sorry 你检查了吗..?
  • 你能提供一个 Swift 的例子吗?
  • @DaniSpringer 是的,当然.. 请检查更新的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多