Handling events in google map at marked or circled locations
Added by Rajeev Gajbhiye over 12 years ago
Hi,
What I observe is that if I draw a circle or polyline, mouse is detected over them which is indicated by change in shape of mouse indicating them to be clickable. However, how can I respond to mouse events over them? There are signals for click, double click and mouse move, but they are quite general which will require me to map coordinates with my markers, etc. If mouse is detected over these marks, is it elegantly possible to handle events over them?
Also, can I remove a particular marker or circle or polyline? I have method to clear overlays, but it will remove all markers and polylines (and not circles).
Regards,
Rajeev.
Replies (2)
RE: Handling events in google map at marked or circled locations - Added by Wim Dumon over 12 years ago
Hello Rajeev,
Wt's googlemaps api does not have mouse events for markers, even though google supports it.
Easiest solution for you is probably to add the missing support in a class that inherits from WGoogleMap. That should look like this:
void myGoogleMap::addMarker() {
std::stringstream strm;
strm << "var position = new google.maps.LatLng("
<< pos.latitude() << ", " << pos.longitude() << ");"
<< "var marker = new google.maps.Marker({"
<< "position: position,"
<< "map: " << jsRef() << ".map"
<< "});"
<< jsRef() << ".map.overlays.push(marker);"
<< jsRef() << ".map.event.addListener(marker, 'event', function(){alert('1');});";
doGmJavaScript(strm.str());
}
Instead of alert('1')
, you trigger a JSignal to forward the signal to C.
Wim.
RE: Handling events in google map at marked or circled locations - Added by Rajeev Gajbhiye over 12 years ago
Hey,
Thanks for the suggestion. I tried it and it worked like a charm. As a side-effect I got to learn bit of javascript and Google Map APIs. However, there is a bit of change in the code that you mentioned. More specifically, it is at the line where listener is being added. That wasn't hard to find once I read streamJSListener() implementation and went through GoogleMap's api.
void myGoogleMap::addMarker() {
std::stringstream strm;
strm << "var position = new google.maps.LatLng("
<< pos.latitude() << ", " << pos.longitude() << ");"
<< "var marker = new google.maps.Marker({"
<< "position: position,"
<< "map: " << jsRef() << ".map"
<< "});"
<< jsRef() << ".map.overlays.push(marker);"
<< "google.maps.event.addListener(marker, 'click', (marker, 'event', function(){alert('1');});";
doGmJavaScript(strm.str());
}
Regards,
Rajeev.