如何在mapview上设置当前位置图钉的监听器?

时间:2015-10-17 13:07:40

标签: 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;
}

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

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

2 个答案:

答案 0 :(得分:3)

您可以使用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;
}

一旦完成。您可以使用现有的委托方法,

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

答案 1 :(得分:0)

您是否尝试过调用GMSMapView的委托方法?

- (void)mapView:(GMSMapView *)mapView
    didTapInfoWindowOfMarker:(GMSMarker *)marker;