问题
I am using the Google Maps API for iOS and I want to make it so when you tap on a GMSCircle it pops up a little thing I coded elsewhere. I have set the circle to "tappable" but I cannot find what I need to set or make to listen for the tap. What do I use?
CLLocationCoordinate2D circleCenter = CLLocationCoordinate2DMake(10,10);
GMSCircle *circ = [GMSCircle circleWithPosition:circleCenter
radius:10];
circ.tappable = true;
[circ setFillColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:.5]];
circ.map = mapView_;
回答1:
You need to use the delegate method didTapOverlay :
- (void) mapView: (GMSMapView *) mapView didTapOverlay: (GMSOverlay *) overlay
Here the parameter overlay indicates the overlay that was tapped. So you need to check if it equals circ.
EDIT : Adding details on how to check for circle within didTapOverlay
When GMSCircle
is added to the map, a corresponding GMSPolygon
is also created. If the circle is set as tappable, then on tapping it, the overlay passed to the didTapOverlay
method is this related polygon and not the circle . So a direct comparison between the overlay and the circle is not possible. Hence as Raspu as pointed out, you can set a value in title using circ.title =
and then inside didTapOverlay, you can check if overlay.title
is same as circ.title
. This works because the title property of the circle is within the corresponding polygon and hence will be present in the overlay parameter.
来源:https://stackoverflow.com/questions/17975273/make-gmscircle-respond-to-tap