Google Maps GWT API - 2.2.1 - Example code for this release (GDirections API)

108 views
Skip to first unread message

aglaforge

unread,
Jun 3, 2007, 3:13:29 PM6/3/07
to Google Web Toolkit

//General setup nothing too interesting
RootPanel.get("slot2").add(searchStatusText);
GMap2Widget mapWidget = new GMap2Widget("500px", "500px");
RootPanel.get("slot4").add(mapWidget);
final GMap2 map = mapWidget.getGmap();
map.addControl(GControl.GSmallZoomControl());
Element e = RootPanel.get("directions").getElement();

//Create the GDirections object and associate it to the map... you can
create it without any map association (which means the API won't
automatically put the route on the map, but you'll still get the
results). Also if you want to use Google's step by step directions on
your page pass a Element object for a div (as in the example below).

final GDirections gd = new GDirections(map, e);


// Stronly recommend that you listen for the "load" event before
attempting to access any of the result objects, lest you will be
burdened with copious amounts of error messages. An example of how to
do that is below.

GEventListener listener = GEvent.addListener(gd, "load", new
GEventHandler(){

public void onHandle(JSObject source, JSObject[] param) {

GDirectionsDistance distance = gd.getDistance();
GDirectionsDuration duration = gd.getDuration();

GRoute route = gd.getRoute(0);
int steps = route.getNumSteps();
GLatLng end = route.getEndLatLng();
distance = route.getDistance();
duration = route.getDuration();
String summary = route.getSummaryHtml();

GStep step = route.getStep(0);
GLatLng step_first = step.getLatLng();
int step_polyline_index = step.getPolylineIndex();
String step_desc = step.getDescriptionHtml();
GDirectionsDistance step_distance = step.getDistance();
GDirectionsDuration step_duration = step.getDuration();

distance = step_distance;
duration = step_duration;

GLog.write("Distance in miles: " + distance.getMiles());
GLog.write("Time in seconds: " + duration.getSeconds());
GLog.write(distance.getHtml());
GLog.write(duration.getHtml());

int days = duration.getDays();
int hours = duration.getHours();
int minutes = duration.getMinutes();
int seconds = duration.getSeconds();

GPlaceMark gpm = gd.getGeocode(0);
String name = gpm.getAddress();
GLatLng pos = gpm.getPoint();
int accuracy = gpm.getAccuracy();
String country = gpm.getCountry();
String state = gpm.getState();
String postalCode = gpm.getPostalCode();
String streetLine = gpm.getStreetLine();
String city = gpm.getCity();
String county = gpm.getCounty();

GMarker m = gd.getMarker(0);
GLatLng ltln = m.getPoint();

GLog.write(gd.getSummaryHtml());
GPolyline gp = gd.getPolyline();
GLatLngBounds bounds = gd.getBounds();
int numRoutes = gd.getNumRoutes();
int numGeocodes = gd.getNumGeocodes();
GGecodeStatus status = gd.getStatus();
int s = status.getCode().getValue();
String request = status.getRequest();


// gd.clear();

}});

//You can specify options which define the behaviour of the
GDirections search
GDirectionsOptions gdo = new GDirectionsOptions();
gdo.setGetPolyline(true);
gdo.setPreserveViewport(true);
gdo.setLocale("fr_CA");

// Option 1 - call the load method
// gd.load("from:Austin, TX to: San Jose", gdo);

//Option 2 - use the waypoints method
String[] locations = {"4807 Fern Hollow, Austin, TX 78731",
"Mountain View, CA 94043"};
gd.loadFromWaypoints(locations, gdo);

//Add in a GTrafficOverlay for good sake
GTrafficOverlay gto = new GTrafficOverlay();
map.addOverlay(gto);
gto.hide();
gto.show();

avantnoisebot

unread,
Jul 24, 2007, 12:35:39 PM7/24/07
to Google Web Toolkit
I'm unable to get this to work at all, and I'm completely stumped.

I have two GLatLng instances, src and dst, and all I want to do is
draw the road-route from one to the other. The following does not
work, maybe you can tell me what I'm doing wrong:

GDirectionsOptions directionOptions directionOptions = new
GDirectionsOptions();
directionOptions.setGetPolyline(false);
directionOptions.setGetSteps(false);
directionOptions.setPreserveViewport(true);
GDirections directions = new GDirections(getGMap());
String[] query = new String[2];
query[0] = src.lat() + "," + src.lng();
query[1] = dst.lat() + "," + dst.lng();
directions.loadFromWaypoints(query, directionOptions);

where getGMap() returns a GMap2.

Thank you for any assistance you can provide.

krispy

unread,
Jul 24, 2007, 5:26:05 PM7/24/07
to Google Web Toolkit
Well, from what I've read of the documentation at www.google.com/apis/maps,
the problem would be:

directionOptions.setGetPolyline(false);

I would say instead:

directionOptions.setGetPolyline(true);

Otherwise, the API won't draw a line from point to point - if you
simply omit the options, this should work too:

directions.loadFromWaypoints(query);

I think the default is to draw a line if you give the GDirections
object a GMap parameter. Look at the docs:

http://www.google.com/apis/maps/documentation/reference.html#GDirections

I haven't actually tried using the API to get directions, so this is
my best guess from what the docs say. HTH!

-krispy

avantnoisebot

unread,
Jul 25, 2007, 7:32:18 AM7/25/07
to Google Web Toolkit
Sorry, that was a cut-and-paste error in my message. I've tried
dozens of variations on all these options, including the ones you
mention and many many more. I just happened to copy a version that
had that particular setting 'false' (which, as I read the docs,
shouldn't make any difference one way or the other if a map is
supplied).

I've been focused on the GWT api and sample code, not the raw
javascript api, but the last suggested to me that I should add
listeners for "error" and "addoverlay" as well as "load" (the GWT
sample code makes no mention of this). Now I finally have more info:
I'm getting the "error" callback. The error description is

The given key is either invalid or does not match the domain for
which it was given.

So what does that mean? What "key" is it referring to?

My current code looks like this:

public void showDirections(GLatLng src, GLatLng dst) {
GDirectionsOptions directionOptions = new
GDirectionsOptions();
directionOptions.setGetPolyline(true);
directionOptions.setGetSteps(false);
directionOptions.setPreserveViewport(true);
directionOptions.setLocale("en_US");
final GDirections directions = new GDirections(getGMap());
GEvent.addListener(directions, "load", new GEventHandler() {


public void onHandle(JSObject source, JSObject[] param) {

System.out.println("load callback");
}
});
GEvent.addListener(directions, "error", new GEventHandler() {


public void onHandle(JSObject source, JSObject[] param) {

System.err.println("error callback");
GGecodeStatus status = directions.getStatus();
System.err.println(status.getCode().getDesc());
}
});
GEvent.addListener(directions, "addoverlay", new


GEventHandler() {
public void onHandle(JSObject source, JSObject[] param) {

System.out.println("addoverlay callback");


}
});
String[] query = new String[2];

query[0] = src.lat() + " " + src.lng();
query[1] = dst.lat() + " " + dst.lng();
System.out.println(query[0]);
System.out.println(query[1]);
directions.loadFromWaypoints(query, directionOptions);
}

The args are:
21.319166 -157.929443
21.318333 -157.929993

which maps.google.com can handle fine.

If you can actually get this to work, let me know, and thanks for the
response in any case.

avantnoisebot

unread,
Jul 25, 2007, 9:26:12 AM7/25/07
to Google Web Toolkit
Following up my own post -- I don't think this GWT Java API works at
all.

Here's a utterly trivial example, using the simple load method and a
canned query string taken directly from the original poster's sample
code. Invocations of this method _always_ fail, with the error:

The given key is either invalid or does not match the domain for
which it was given.


public static void showDirections(GMap2 map) {
final String query = "from:Austin, TX to: San Jose";
final GDirections directions = new GDirections(map);


GEvent.addListener(directions, "load", new GEventHandler() {
public void onHandle(JSObject source, JSObject[] param) {

System.out.println("Directions " +query+ ": load
succeeded");


}
});
GEvent.addListener(directions, "error", new GEventHandler() {
public void onHandle(JSObject source, JSObject[] param) {

GGecodeStatus status = directions.getStatus();
System.err.println("Error get directions " +query+ ":
" +status.getCode().getDesc());


}
});
GEvent.addListener(directions, "addoverlay", new
GEventHandler() {
public void onHandle(JSObject source, JSObject[] param) {

System.out.println("Directions " +query+ ": addoverlay
succeeded");
}
});
directions.load(query);
}

krispy

unread,
Jul 25, 2007, 10:07:07 AM7/25/07
to Google Web Toolkit
avantnoisebot, in order to use the Maps API, you have to have a valid
key for your website, otherwise, you'll get the error you're seeing:

http://www.google.com/apis/maps/signup.html
http://www.google.com/apis/maps/documentation/index.html#Introduction

The API works just fine, myself and many others use it just fine - but
you have to get a key and put it in your host page first before
anything will work!

-krispy

avantnoisebot

unread,
Jul 25, 2007, 4:21:33 PM7/25/07
to Google Web Toolkit
My key is fine, maps are fine, polylines are fine, markers are fine --
everything is fine except this one function. If I had no maps at all,
I would have said so.

krispy

unread,
Jul 26, 2007, 10:09:17 AM7/26/07
to Google Web Toolkit
Ok, I just tried it and it works fine for me - I copy/pasted the
trivial example you have above and it works flawlessly. Could we see
your HTML file that you're using to host the app with?

> My key is fine, maps are fine, polylines are fine, markers are fine --
> everything is fine except this one function. If I had no maps at all,
> I would have said so.

>From a previous post:


> Following up my own post -- I don't think this GWT Java API works at
> all.

I'm sorry that I jumped to conclusions, but it sounded like you were
saying nothing works. And to be fair, you never mentioned that you
knew what that error meant, so I thought it was a basic problem with
your key (you'd be surprised by all the people who post code snippets
and ask why it doesn't work when they don't even have a key yet!).
I'd like to see your HTML file because it seems like there must be
something wrong there - either the version number you're using or
something similar. Also, it's possible that you made too many
geocoding requests - I think the limit is 50,000 or so per day (but it
can add up quickly if you get stuck in infinite recursion/looping
while testing). HTH!

-krispy

> >http://www.google.com/apis/maps/signup.htmlhttp://www.google.com/apis...

avantnoisebot

unread,
Jul 27, 2007, 9:19:05 AM7/27/07
to Google Web Toolkit
Every gwt map api other than GDirections worked with the key I
generated a few months ago, so I figured it was fine. But apparently
not: I created a new key for the same url and now GDirections works.
Seems weird to me that different api's would have different key
requirements, but in any case, thanks for the suggestion.

pamela (Google employee)

unread,
Jul 27, 2007, 5:33:06 PM7/27/07
to Google Web Toolkit
Hi there-

I'm visiting from the Maps API forum to offer some insight here. When
a map is run off the local file system (usually when status bar says
file:///), the key check is not performed. However, both the
GClientGeocoder and GDirections will do their own key checks no matter
where the map is run from. Based on what you've described, I'm
wondering if it's because you're running off a local system and not a
server.

If this isn't the case, then I'm not sure what the problem was and I
apologize for the hassle.
- pamela

GWT-Beginner

unread,
Jul 31, 2007, 10:57:28 AM7/31/07
to Google Web Toolkit
Pamela,
Thanks for the insight. Is there a way we can use geocoding on a
test server which is behind a firewall and not reachable from outside.
This is for development purposes. I hope you understand that deploying
changes to an external server during development can be cumbersome.

thanks.

On Jul 27, 2:33 pm, "pamela (Google employee)"

GWT-Beginner

unread,
Aug 3, 2007, 8:30:07 PM8/3/07
to Google Web Toolkit
Anybody has run into the same issue? Any workarounds?
Reply all
Reply to author
Forward
0 new messages