Project

General

Profile

Using new Google Map API

Added by Andrew Craig over 13 years ago

Hi.

I am trying to work on my own Google Map widget and was using the WGoogleMap as a base example. It was working fine until I tried to start using the version 3 of the Google Map API ( version 2 is being depreciated. ).

I tried to strip out everything in my work and eventually got it down to a very simple program.

This will produce an error in Chrome:

Uncaught TypeError: Cannot set property 'ondragstart' of null

#include <Wt/WApplication>

using namespace Wt;

class HelloApplication : public WApplication
{
public:
  HelloApplication(const WEnvironment& env);
};

HelloApplication::HelloApplication(const WEnvironment& env)
  : WApplication(env)
{
  setTitle("Hello world");

  const std::string uri = "http://maps.google.com/maps/api/js?sensor=false";
  require(uri);
}

WApplication *createApplication(const WEnvironment& env)
{
  return new HelloApplication(env);
}

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

Replies (2)

RE: Using new Google Map API - Added by Koen Deforche over 13 years ago

Hey,

It seems there is a special option to load the url through AJAX.

The version attached below works for me (and switches between the two forms for dealing with the progressive bootstrap).

However, we use the generalized google API JavaScript load mechanism now in WGoogleMap, and specifically ask to load version 2.

It seems that by changing in WGoogleMAp.C line 170-171, I can load the new maps API version, but then we fail somewhere in the

initialize() function, possibly because of changes in the API ?

      "google.load(\"maps\", \"2\", "
      ""          "{Other_params:\"sensor=false\", callback: initialize});"

to

      "google.load(\"maps\", \"3\", "
      ""          "{\"other_params\":\"sensor=false\", \"callback\": initialize});"

#include <Wt/WApplication>
#include <Wt/WEnvironment>

using namespace Wt;

class HelloApplication : public WApplication
{
public:
  HelloApplication(const WEnvironment& env);
};

HelloApplication::HelloApplication(const WEnvironment& env)
  : WApplication(env)
{
  setTitle("Hello world");

  std::string uri;
  if (env.ajax()) {
    doJavaScript("window.initialize = function() { }", false);
    uri = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
  } else
    uri = "http://maps.google.com/maps/api/js?sensor=false";

  require(uri);
}

WApplication *createApplication(const WEnvironment& env)
{
  return new HelloApplication(env);
}

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

RE: Using new Google Map API - Added by Wim Dumon over 13 years ago

WGoogleMaps now support the v3 API. Changes are in the public git and will be in the next release.

Wim.

    (1-2/2)