Hey Richard,

2009/1/29 Richard Ulrich <[email protected]>:
> So I started playing with the GoogleMaps widget.
>
> But I can't seem to get it running.
> Here I what I do :

Thanks for the start!

I moved some things around, and made the following changes:
 - the widget is created but not yet inserted in the widget tree by
only doing the JavaScript as soon as the widget is load()ed
 - there is no need for onLoadCallback: this is integrated in Wt --
JavaScript is only run when a library is loaded
 - make sure that the map is rerendered when the session is refreshed

And that seems to work!

Regards,
koen

Here is my test program:

#include <Wt/WApplication>
#include <Wt/WContainerWidget>
#include <Wt/WText>

#include <string>
#include <sstream>

using namespace Wt;

class WGoogleMap : public WContainerWidget
{
public:
  WGoogleMap(WContainerWidget *parent = 0);
  virtual ~WGoogleMap() { }

  virtual void load();
  virtual void refresh();
};

// example javascript code from :
// http://code.google.com/apis/maps/documentation/

WGoogleMap::WGoogleMap(WContainerWidget *parent) : WContainerWidget(parent)
{
  WApplication *app = WApplication::instance();

  // if there is no google api key configured, use the one
  // for http://localhost:8080/
  const std::string localhost_key =
    "ABQIAAAAWqrN5o4-ISwj0Up_depYvhTwM0brOpm-All5BF6PoaKBxRWWERS"
    "-S9gPtCri-B6BZeXV8KpT4F80DQ";

  std::string googlekey;
  if (!app->readConfigurationProperty("google_api_key", googlekey))
    googlekey = localhost_key;

  // load the google javascript api script
  const std::string gmuri = "http://www.google.com/jsapi?key="; + googlekey;
  app->require(gmuri, "google");
}

void WGoogleMap::load()
{
  refresh();
}

void WGoogleMap::refresh()
{
  // initialize the map
  std::ostringstream strm;
  strm << "function initialize() {"
       << "    var map = new google.maps.Map2(" << jsRef() << ");"
       << "    map.setCenter(new google.maps.LatLng(47.01887777,
8.651888), 13);"
       << "    map.enableScrollWheelZoom();"
       << "    map.addControl(new google.maps.HierarchicalMapTypeControl());"
       << "}";
  strm << "google.load(\"maps\", \"2\",
{other_params:\"sensor=false\", callback: initialize});";

  WApplication::instance()->doJavaScript(strm.str());
}

WApplication *createApplication( const WEnvironment& env )
{
  WApplication *app = new WApplication(env);

  app->root()->addWidget(new WGoogleMap());

  return app;
}

int main( int argc, char **argv )
{
   return WRun( argc, argv, &createApplication );
}

------------------------------------------------------------------------------
This SF.net email is sponsored by:
SourcForge Community
SourceForge wants to tell your story.
http://p.sf.net/sfu/sf-spreadtheword
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to