如何在 mapview 上为当前位置 pin 设置侦听器?
How to set listener for current location pin on mapview?
这是我目前所做的:
_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);
我的观点是:
我怎样才能有一个侦听器,在用户单击当前位置图钉时进行捕获。我试过了,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];
您是否尝试调用 GMSMapView 的委托方法?
- (void)mapView:(GMSMapView *)mapView
didTapInfoWindowOfMarker:(GMSMarker *)marker;
您可以使用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];
}
这是我目前所做的:
_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);
我的观点是:
我怎样才能有一个侦听器,在用户单击当前位置图钉时进行捕获。我试过了,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];
您是否尝试调用 GMSMapView 的委托方法?
- (void)mapView:(GMSMapView *)mapView
didTapInfoWindowOfMarker:(GMSMarker *)marker;
您可以使用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];
}