如何以编程方式选择地图引脚

时间:2013-04-02 07:00:53

标签: iphone ios objective-c map mkmapview

我是Maps Concept的新手。

请查看附件一次。

enter image description here

当我点击“pin”时,我收到消息说“欣赏你的笑容”或任何文字.....现在我想要...当我们选择表格视图时,我需要提出该消息pin(没有用户与该引脚的交互)...

我正在使用以下代码来显示此地图和图钉。

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

现在我将在下面显示所有引脚。

enter image description here

怎么做当我们点击表视图时,我需要显示该引脚的标题,就像我们选择引脚时的显示方式一样?

先谢谢

2 个答案:

答案 0 :(得分:29)

您正在寻找的方法是 selectAnnotation:

didSelectRowAtIndexPath中,您必须找到与表格行相关联的注释。

找到后,您可以告诉它,使用以下方法选择它:

[mapView selectAnnotation:foundAnnotation animated:YES];

答案 1 :(得分:3)

与Objective C答案相同,在tableview中有一个注释列表。在didSelectRow中的Swift:

对于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)

简短而简单。