//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();
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.
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
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.
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);
}
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
> 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...
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
thanks.
On Jul 27, 2:33 pm, "pamela (Google employee)"