Jason Rosenbaum
Jason Rosenbaum

Reputation: 43

Phonegap/Cordova + MapKit on iOS: Execute javascript function when a pin is tapped

I'm using Phonegap/Cordova and its MapKit plugin to build a HTML/javascript based app on iOS.

I'd like to execute a javascript function when a pin is clicked on the map, ideally instead of having the pin show its default annotation. (And also passing in some data about which pin is clicked, it's ID I guess.)

It doesn't look like there's any callback type features for the pin tap event (there isn't really a pin tap event), only once the more info-type button on the annotation is clicked.

Anyone know if this functionality exists, or how I might go about adding it?

Upvotes: 1

Views: 632

Answers (1)

Jason Rosenbaum
Jason Rosenbaum

Reputation: 43

I've answered my own questions -- here's what I ended up putting in MapKit.m:

//when a pin is selected, do something
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    NSString *annotationTapFunctionString = [NSString stringWithFormat:@"%s%@%s", "annotationTap('", [view.annotation title], "')"];
    [self.webView stringByEvaluatingJavaScriptFromString:annotationTapFunctionString];
}

When a pin is tapped, it calls the annotationTap javascript function with the pin's title as an argument.

Upvotes: 3

Related Questions