开发者

Show callout when tapping overlay

I have an MKMapVie开发者_如何学Gow with several overlays. Works all just fine, and it's incredible how simple it works. There is, however, one thing I can't get to work. The idea is simple: when a user taps within the area that is covered by an overlay, a callout with some information about that overlay has to come up. The overlays are all MKPolygons, which follow the MKOverlay protocol and therefore the MKAnnotation protocol.

The MKOverlay protocol conforms to the MKAnnotation protocol. As a result, all overlay objects are also annotation objects and can be treated as one or both in your code. If you opt to treat an overlay object as both, you are responsible for managing that object in two places. If you want to display both an overlay view and annotation view for it, you must implement both the mapView:viewForOverlay: and mapView:viewForAnnotation: methods in your application delegate. It also means that you must add and remove the object from both the overlays and annotations arrays of your map.

This comes from the Apple docs. I tried something like this:

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

      MKAnnotationView *aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
      aView.canShowCallout = YES;
}

But that doesn't seem to work. I've tried using gesture recognizers, but I have no idea how to show a callout other than by using the canShowCallOut property...


I suppose you have to add the MKOverlays as annotations also

 [self.mapView addAnnotations:myOverlays];

Then return a MKAnnotationView in (mapView:viewForAnnotation) that's not hidden, either a graphic (tap-able) or zero alpha view. Next, add a UITapGestureRecognizer for each MKOverlayView, make sure it works with the map's gestures (UIGestureRecognizerDelegate implementation for simultaneous recognition). Finally when your gesture recognizer fires do this

[self.mapView setSelectedAnnotations:[NSArray arrayWithObject:myOverlayView.overlay]];

I'm not certain that this actually triggers the callOut showing though. Also make sure your return title and/or subtitle from your overlay object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜