Project

General

Profile

[WGoogleMap] Launch custom JavaScript function from button in InfoWindow

Added by Lucas Vitorino over 12 years ago

Hi all,

I'm asking for your help because I'm struggling and can't seem to find the answer.

My map has an InfoWindow with an HTML button in it. I have a custom function I'd like to run on clicking the button.

However, the JS code in the "onclick" field on the button can't see the JS code that includes the variables (map, markers) that I want to modify, nor the function I'd like to run.

Clicking the marker generates an event I can handle with C code. The things is that JS code I write and execute in this handler doesn't see the JS code with the map and the markers. Maybe I could trigger a JS event whose handler would run the function, but I don't know how to do that.

Do you see way of connecting the JS run by clicking this button to my main piece of JS code ?

Thanks in advance for your help.

Lucas


Replies (9)

RE: [WGoogleMap] Launch custom JavaScript function from button in InfoWindow - Added by Lucas Vitorino over 12 years ago

I would really appreciate if somebody more skilled in JS and Wt than me tried to help.

I could give some really cool music as a bounty.

Please ?

Lucas

RE: [WGoogleMap] Launch custom JavaScript function from button in InfoWindow - Added by Mohammed Rashad over 12 years ago

Have you seen Javascript examples in examples/javascript?

Your map variable should not have var;

or try

window.mapInstance = google map()

doJavaScript is what you need.

I am also dealing with maps in many application. For now I use OpenLayers which will be soon replaced by own renderers?

RE: [WGoogleMap] Launch custom JavaScript function from button in InfoWindow - Added by Lucas Vitorino over 12 years ago

Hi Mohammed,

I found someone on stackoverflow that apparently had the same issue than me and acknowledged the "var" thing was not the way to go.

I have read things about scoping in JS (because scoping is obviously the issue), but haven't been able to solve the issue. The thing is that the WGoogleMap widget is coded with a "var map = new google.maps.Map(self)".

Do I have to recode a WGoogleMap widget to be able to call a function from an InfoWindow ?

May I kindly ask how you integrate your maps with Wt for your applications ?

Thank you very much,

Lucas

RE: [WGoogleMap] Launch custom JavaScript function from button in InfoWindow - Added by Mohammed Rashad over 12 years ago

Lucas Vitorino wrote:

Hi Mohammed,

I found someone on stackoverflow that apparently had the same issue than me and acknowledged the "var" thing was not the way to go.

I have read things about scoping in JS (because scoping is obviously the issue), but haven't been able to solve the issue. The thing is that the WGoogleMap widget is coded with a "var map = new google.maps.Map(self)".

Yes you need to recode to use google maps

Do I have to recode a WGoogleMap widget to be able to call a function from an InfoWindow ?

May I kindly ask how you integrate your maps with Wt for your applications ?

As stated in previous mail I use OpenLayers openlayers.org

Documentation is bad but there are tons of examples for OL http://openlayers.org/dev/examples/

OL can render Google,Bing,OSM, your own WMS layers and vector layers

I am not authorised to give you source of my VROpenLayers Widget. But I can explain you how to do it

Things are simple. In a WCompositeWidget class add some JS to show the map

wapp->require("http://openlayers.org/api/OpenLayers.js");

std::stringstream createmap_;

createmap_ <<

"function initMap() {"

"map = new OpenLayers.Map('" + this->id()+ "',{"

"controls: [new OpenLayers.Control.PanZoom()]});"

"}initMap();";

doJavaScript(createmap_);

Thank you very much,

Lucas

Regards,

Rashad

RE: [WGoogleMap] Launch custom JavaScript function from button in InfoWindow - Added by Lucas Vitorino over 12 years ago

Cheers such a bunch.

I'll have to create such a widget soon, the help you gave me is very appreciated.

Just for you to know, a workaround I found 5 minutes ago is attaching the function and the objects I want to use to the Wt javascript object, accessible everywhere.

To do so quite cleanly, you just need to execute :

Wt.dummyWhatever = new Object();

and then

Wt.dummyWhatever.dummyFunction = function(val1, val2){xxxxx};

That way, I can execute the function from the html button in the infoWindow.

Thanks again.

Regards,

Lucas

RE: [WGoogleMap] Launch custom JavaScript function from button in InfoWindow - Added by Wim Dumon over 12 years ago

Lucas,

I'm not 100% what exactly your problem is, so I'll try a few answers.

1. If you say that in your clicked() callback you don't have a pointer to the WGoogleMap class that caused the click, you should change the way you bind your callback to your clicked handler. There's always a way to solve this with boost::bind:

button->clicked().connect(boost::bind(SomeClass::myFunction, someClassInstance, myGoogleMapsInstance));

where myFunction is a member of SomeClass with a WGoogleMaps pointer as parameter.

2. If you say that you want access to the JavaScript google maps object, I must say that this is indeed not documented. However, in the current version of Wt, you can access it as follows:

myMap~~doJavaScript("var googleMapsObject = " + myMap~~>jsRef() + ".map;");

jsRef() returns a string that can be used in JS code to refer to the widget object. We should probably add a jsMapRef() to WGoogleMaps to refer to the google map object.

Hope this helps,

Wim.

RE: [WGoogleMap] Launch custom JavaScript function from button in InfoWindow - Added by Lucas Vitorino over 12 years ago

Wim,

Thanks very much for the help, and sorry for the delay in the answer.

My problem was actually a very common JS scope problem : by clicking a HTML button in a InfoWindow, I wanted to trigger a function defined in the main JS code. The problem was that I couldn't access the function because of the namespace. The solution I found was simple : attaching the function and the variable to an JS object accessible everywhere : Wt.

You're right : I could have just as well attached the functions and the objects to the map object, accessing it with myMaps->jsRef + ".map" ; I hadn't thought about it, that's clever, cheers for that.

I'm out of trouble with that issue now.

Your first answer is cool, I'm not yet familiar with the boost::bind methods. But I get your point.

Anyway, thank you very much for your time, it's definitely appreciated.

Cheers,

Lucas

RE: [WGoogleMap] Launch custom JavaScript function from button in InfoWindow - Added by Wim Dumon over 12 years ago

Lucas,

It's probably a bad idea to put anything in a global location like Wt. Some advantages of storing JS data in an object's jsRef(), is that (1) it disappears when the object is removed in question and (2) your solution will be scalable: if you instantiate more than one of your class, they won't both be usingt he same global variable in some global location.

Regards,

Wim.

RE: [WGoogleMap] Launch custom JavaScript function from button in InfoWindow - Added by Lucas Vitorino over 12 years ago

Wim,

You're definitely right. I'll update my code.

Thanks again.

Regards,

Lucas

    (1-9/9)