开发者

How to add Button to userLocation callout?

开发者 https://www.devze.com 2023-01-06 23:17 出处:网络
How can I display a DisclosureButton in the userLocation (the blue dot) callout like the Apple Maps app does?

How can I display a DisclosureButton in the userLocation (the blue dot) callout like the Apple Maps app does?

Now I just can change its title and subtitle, but I hope to provide custom callout views. Therefore, if I click the blue dot, it can display a button besides only title and subtitle. In this way, when I click the button, it can guide me to another View. I returned MKAnnotationView in viewForAnnotation when annotation == self.mapView.userLocation. However, the callout can display updated title and subtitle, but cannot show the button I added.

I really need this function, and I desire any help and sugg开发者_如何学Cestion!


I've tried camdaochemgio's solution, but it works only for the second tap. Here is a solution which will always work:

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    for (MKAnnotationView* view in views)
    {
        if ([view.annotation isKindOfClass:[MKUserLocation class]])
        {
            view.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        }
    }
}


For the annotationView, set

myAnnotationView.canShowCallout=YES;

then add up to two buttons with

myAnnotationView.leftCalloutAccessoryView=myLeftButton;

myAnnotationView.rightCalloutAccessoryView=myRightButton;

The buttons can be a normal button, a custom button, or one of the predefined types. Like this:

myAnnotationView.rightCalloutAccessoryView=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];

Then add the calloutAccessoryControlTapped: method to handle taps.


maybe late. Here is solution. You implement the UIMapViewDelegete method as below:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    if ([view.annotation isKindOfClass:[MKUserLocation class]] && !view.rightCalloutAccessoryView) {
        view.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消