gpt4 book ai didi

ios - 检测触摸了哪个特定的 map 图钉

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:03:27 29 4
gpt4 key购买 nike

我有一个带有大量图钉的 map View ,每个图钉都有唯一的注释。该 map 还显示了用户位置的脉动蓝点。到目前为止,我只能确定是否触摸了一个引脚,但不能确定被触摸的特定引脚。

如何确定用户触摸的 map 中的特定图钉?我正在使用 Xcode v6.1。示例代码(针对多个引脚之一):

- (void)viewDidLoad {

[super viewDidLoad];

//** Data for Location 1 **
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 45.5262223;
region.center.longitude = -122.63642379999999;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;

[mapView setRegion:region animated:YES];

[self.mapView setDelegate:self];

DisplayMap *ann = [[DisplayMap alloc] init];
ann.title = @"This is Location 1";
ann.subtitle = @"1234 North Main Street";
ann.coordinate = region.center;
[self.mapView addAnnotation:ann];

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

NSLog(@"This logs when any pin is touched, need to know which pin");

}

最佳答案

didSelectAnnotationView 中,传递给方法的 view 参数包含对其关联的注释的引用。

在使用引用之前,您应该检查它是什么类型(例如使用isKindOfClass)并进行相应的处理。这是因为当点击任何注释时将调用委托(delegate)方法,其中包括类型为 MKUserLocation 的用户位置蓝点。您的自定义注释对象也可能具有非标准属性,并且尝试在错误类型的注释上访问这些属性将导致异常。

例子:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {

//Annotation that was selected is in the view parameter...
id<MKAnnotation> annSelected = view.annotation;

//See if this annotation is our custom type (DisplayMap)
//and not something else like MKUserLocation...
if ([annSelected isKindOfClass:[DisplayMap class]])
{
//Now we know annSelected is of type DisplayMap
//so it's safe to cast it as type DisplayMap...
DisplayMap *dm = (DisplayMap *)annSelected;

NSLog(@"Pin touched: title=%@", dm.title);
}
}

关于ios - 检测触摸了哪个特定的 map 图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26684526/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com