ios - 如何在 mapview 上为当前位置 pin 设置监听器?

标签 ios objective-c mkmapview

这是我到目前为止所做的:

_mapview.delegate = self;
_mapview.showsUserLocation = YES;
locationManager = [CLLocationManager new];
locationManager.delegate = self;
locationManager.distanceFilter = kCLLocationAccuracyKilometer;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//iOS 8 API change
if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
    [locationManager requestWhenInUseAuthorization];
}else{
    [locationManager startUpdatingLocation];
}
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
self.mapview.region = MKCoordinateRegionMakeWithDistance(coordinate, 1000, 1000);

我得到了这个观点:

enter image description here

我怎样才能有一个监听器,在用户单击当前位置图钉时进行捕获。我试过了,calloutAccessoryControlTapped 不工作。

更新:

我改成了这样:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
    NSLog(@"MKAnnotationView");

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"];
    annView.canShowCallout = YES;
    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annView.rightCalloutAccessoryView=detailButton;
}

但稍后我需要获取当前位置地址。但标题不是它的地址。如何在 pin 中显示当前位置地址,或者当用户单击 pin 时,我会得到像门牌号这样的地址。街道名称。城市名。状态:

static NSString *defaultPinID = @"pin";
MKPinAnnotationView *pinAnnotation = nil;
pinAnnotation = [[MKPinAnnotationView alloc] initWithAnnotation:view.annotation reuseIdentifier:defaultPinID];

self.address = view.annotation.title;
CLLocationCoordinate2D coor = [view.annotation coordinate];
_eventGeoLocation2 = [PFGeoPoint geoPointWithLatitude:coor.latitude longitude:coor.longitude];

最佳答案

您可以使用 MKPinAnnotationView 提供的默认细节披露按钮。这是代码,

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{

      MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"];
      annView.canShowCallout = YES;
      UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
      annView.rightCalloutAccessoryView=detailButton;
}

完成后。您可以像这样使用现有的委托(delegate)方法,

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
   //Here, the annotation tapped can be accessed using view.annotation

    NSLog(@"%@",view.annotation.title);
    NSLog(@"%@",view.annotation.subtitle);
   [self performSegueWithIdentifier:@"Your identifier" sender:view.annotation.title];
}

关于ios - 如何在 mapview 上为当前位置 pin 设置监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33186975/

相关文章:

ios - 我可以存储 paypal 信息,例如 paypal 的用户 ID 和密码以及用于自动充值功能的信用卡详细信息吗?

ios - 类型 [TeamModel] 没有下标成员

ios - 为什么阴影半径也会对圆的内部产生影响?

objective-c - 关于 MKMapView 的叠加问题

ios - SKTileMapNode:检测触摸的 Tile?

ios - NSDictionary 查找 keyPath

objective-c - 如何从界面生成器访问窗口 View ?

objective-c - NSJSONSerialization 中不可变的 NSJSONReadingOptions

iphone - 使用MKMapView时如何设置精度和距离过滤器

ios - 将注释应用于 map 工具包