Hey Richard,

2009/2/2 Richard Ulrich <[email protected]>:
> Hi Koen,
>
>> My fault, the version below fixes that (you should not pass on the
>> parent widget to the base class if you are relying on the virtual
>> load() method to be called when the widget is inserted in the widget
>> hierarchy).
> This works now everywhere I tested.
>
>> I've modified the test case so that the variable will be available as
>> jsRef() + ".map".
> That's great news. But I couldn't figure out how to use it correctly.
> Here is what I tried:

Oops... the problem is that you cannot manipulate the map before
initialize is called. But you also cannot call google.load() multiple
times since that loads google's JavaScript multiple times (despite
their documentation saying it will not).

In our development branch we have added a new method to WWidget,
render(), which is useful to defer rendering code until the last
moment, which would be useful in this case. But in Wt 2, we do have
the undocumented prepareRender() method which can be used here.

The version below shows a marker (at your home, I assume?)

Regards,
koen

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

 void addMarker(const std::pair<double, double> &pos);

 void refresh();

protected:
 virtual void prepareRerender();

private:
 bool                     rendered_;
 std::vector<std::string> additions_;
};

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

WGoogleMap::WGoogleMap(WContainerWidget *parent)
 : WContainerWidget(),
   rendered_(false)
{
 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");

 if (parent)
   parent->addWidget(this);
}

void WGoogleMap::refresh()
{
 rendered_ = false;
}

void WGoogleMap::prepareRerender()
{
 if (!rendered_) {
   // initialize the map
   std::ostringstream strm;
   strm << "function initialize() {"
        << "    var div = " << jsRef() << ";"
        << "    var map = new google.maps.Map2(div);"
        << "    map.setCenter(new google.maps.LatLng(47.01887777,
8.651888), 13);"
        << "    map.enableScrollWheelZoom();"
        << "    map.addControl(new google.maps.HierarchicalMapTypeControl());"
        << "    div.map = map;";
   std::copy(additions_.begin(), additions_.end(),
             std::ostream_iterator<std::string>(strm));
   strm << "}";
   strm << "google.load(\"maps\", \"2\",
{other_params:\"sensor=false\", callback: initialize});";

   additions_.clear();

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

   WContainerWidget::prepareRerender();
   rendered_ = true;
 }
}

void WGoogleMap::addMarker(const std::pair<double, double> &pos)
{
 std::ostringstream strm;
 strm << "{var marker = new google.maps.Marker(new "
   "google.maps.LatLng(47.01887777, 8.651888));"
      <<  jsRef() << ".map.addOverlay(marker);}";

 const std::string jsstr = strm.str();

 if (rendered_)
   WApplication::instance()->doJavaScript(jsstr);
 else
   additions_.push_back(jsstr);
}

------------------------------------------------------------------------------
Create and Deploy Rich Internet Apps outside the browser with Adobe(R)AIR(TM)
software. With Adobe AIR, Ajax developers can use existing skills and code to
build responsive, highly engaging applications that combine the power of local
resources and data with the reach of the web. Download the Adobe AIR SDK and
Ajax docs to start building applications today-http://p.sf.net/sfu/adobe-com
_______________________________________________
witty-interest mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/witty-interest

Reply via email to