DirectionsRenderer.setDirections does nothing with a DirectionsResult I've pulled out of my database

2,161 views
Skip to first unread message

Stewart

unread,
Aug 30, 2010, 3:33:10 AM8/30/10
to Google Maps JavaScript API v3
Hi All -

I'm building a webapp where I want to store a stringified
DirectionsResult into my DB, and then pull it out later on and show it
on a map at a later time.

I've got a sample application here: http://bolidian.com/samples/gmapi/test.html

As you can guess, it's not working. :-/

When I first use a DirectionsService to get the DirectionsResult and
pass it to a DirectionsRenderer then, it works fine. I then stringify
the DirectionsResult and put in my DB, then pull it out, destring it
(using json2.js) and call setDirections on a DirectionsRenderer: no
response. The DirectionsRenderer doesn't show anything.

My javascript console shows this error:

Error: b[Kb] is not a function
Source File: http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/2/main.js
Line: 85

I don't understand what this error means or why it's there -- and that
JS code is heavily minified so I can't really understand it.

Note that I've carefully compared the DirectionsResult going into and
out of the DB, and they're identical char-by-char.

Any help would be most appreciated! All the code is at that link
above.

Thanks,
Stewart

geoco...@gmail.com

unread,
Aug 30, 2010, 1:13:01 PM8/30/10
to Google Maps JavaScript API v3
On Aug 30, 12:33 am, Stewart <stew...@bolidian.com> wrote:
> Hi All -
>
> I'm building a webapp where I want to store a stringified
> DirectionsResult into my DB, and then pull it out later on and show it
> on a map at a later time.

Why not get it when you need it?

>
> I've got a sample application here:http://bolidian.com/samples/gmapi/test.html
>
> As you can guess, it's not working. :-/
>
> When I first use a DirectionsService to get the DirectionsResult and
> pass it to a DirectionsRenderer then, it works fine. I then stringify
> the DirectionsResult and put in my DB, then pull it out, destring it
> (using json2.js) and call setDirections on a DirectionsRenderer: no
> response. The DirectionsRenderer doesn't show anything.
>
> My javascript console shows this error:
>
> Error: b[Kb] is not a function
> Source File:http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/2/main.js
> Line: 85
>
> I don't understand what this error means or why it's there -- and that
> JS code is heavily minified so I can't really understand it.
>
> Note that I've carefully compared the DirectionsResult going into and
> out of the DB, and they're identical char-by-char.

Not anymore. When I do the same directions query, the "internal"
names the API uses are different (there must have been a release since
you captured your sample). Since the json object contains these
internal names, I have to assume that this is not something that is
intended to be done, you will certainly have to lock down the version
of the API for it to continue working (since you don't specify the
verison in the script include, you are currently getting the
development version, I think..).

I also see character encoding problems in your version (? instead of
the copyright symbol).

I wouldn't think this kind of use would be allowed by the Terms of
Use, but you are displaying it on a map, so it may be OK (and IANAL),
it certainly seems to not be anticipated.

-- Larry

Stewart

unread,
Aug 30, 2010, 9:44:55 PM8/30/10
to Google Maps JavaScript API v3


On Aug 31, 3:13 am, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
> On Aug 30, 12:33 am, Stewart <stew...@bolidian.com> wrote:
>
> > Hi All -
>
> > I'm building a webapp where I want to store a stringified
> > DirectionsResult into my DB, and then pull it out later on and show it
> > on a map at a later time.
>
> Why not get it when you need it?

The app needs to be able to get a route (from a DirectionsService) and
then show that same route every time (days, weeks, months later). If I
request a new DirectionsResult every time then I can potentially get a
different route each time.

> > I've got a sample application here:http://bolidian.com/samples/gmapi/test.html
>
> > As you can guess, it's not working. :-/
>
> > When I first use a DirectionsService to get the DirectionsResult and
> > pass it to a DirectionsRenderer then, it works fine. I then stringify
> > the DirectionsResult and put in my DB, then pull it out, destring it
> > (using json2.js) and call setDirections on a DirectionsRenderer: no
> > response. The DirectionsRenderer doesn't show anything.
>
> > My javascript console shows this error:
>
> > Error: b[Kb] is not a function
> > Source File:http://maps.gstatic.com/intl/en_us/mapfiles/api-3/2/2/main.js
> > Line: 85
>
> > I don't understand what this error means or why it's there -- and that
> > JS code is heavily minified so I can't really understand it.
>
> > Note that I've carefully compared the DirectionsResult going into and
> > out of the DB, and they're identical char-by-char.
>
> Not anymore.  When I do the same directions query, the "internal"
> names the API uses are different (there must have been a release since
> you captured your sample).  Since the json object contains these
> internal names, I have to assume that this is not something that is
> intended to be done, you will certainly have to lock down the version
> of the API for it to continue working (since you don't specify the
> verison in the script include, you are currently getting the
> development version, I think..).

It can't be a different version of the API -- I did all this in the
space of about 3 hours yesterday. Same results today.

> I also see character encoding problems in your version (? instead of
> the copyright symbol).

I don't see that -- where are you seeing it?

> I wouldn't think this kind of use would be allowed by the Terms of
> Use, but you are displaying it on a map, so it may be OK (and IANAL),
> it certainly seems to not be anticipated.

I didn't see anything in the conditions that said it's not okay to
store the DirectionsResult for later use. I can't see why it would be
a problem -- ultimately it means less calls on the DirectionsService,
which is a good thing, right? (Though that's not why I'm doing it -- I
actually need the same set of directions for a long period of time
[see above]).

Stewart

geoco...@gmail.com

unread,
Aug 31, 2010, 2:34:43 AM8/31/10
to Google Maps JavaScript API v3
I compared the stringified json object you display with the one I got
from the same directions query earlier today.

>
> > I wouldn't think this kind of use would be allowed by the Terms of
> > Use, but you are displaying it on a map, so it may be OK (and IANAL),
> > it certainly seems to not be anticipated.
>
> I didn't see anything in the conditions that said it's not okay to
> store the DirectionsResult for later use. I can't see why it would be
> a problem -- ultimately it means less calls on the DirectionsService,
> which is a good thing, right? (Though that's not why I'm doing it -- I
> actually need the same set of directions for a long period of time
> [see above]).

I can't answer the questions about the legality, that you will have to
decide for yourself or consult a lawyer about.

Regardless of that, the json object contains internal names from the
particular API version, so you will need to specify a version, and
change your code whenever that version becomes obsolete.

-- Larry

>
> Stewart

doogs

unread,
Aug 31, 2010, 3:03:57 AM8/31/10
to Google Maps JavaScript API v3
Hey Stewart

Just wondering if you ever found a resolution to this.

I'm attempting the same thing and would love to be able to store
DirectionResults in my DB to have the DirectionsRenderer draw at a
later stage.

I get the same script error.

Thanks.

William

unread,
Aug 31, 2010, 3:03:03 AM8/31/10
to Google Maps JavaScript API v3
On Aug 30, 5:33 pm, Stewart <stew...@bolidian.com> wrote:
>
> I've got a sample application here:http://bolidian.com/samples/gmapi/test.html
>

as an example, the above application has the following json fragment:

"start_location":{"b":-32.06027,"c":115.89242}

this represents a google.maps.LatLng object, so the parser will need
to use a
reviver function to recreate the objects by calling:

new google.maps.LatLng(lat,lng) with these parameters:

"start_location":{"lat":-32.06027,"lng":115.89242}

...

Stewart

unread,
Aug 31, 2010, 6:38:18 AM8/31/10
to Google Maps JavaScript API v3
Hi William -

Are you saying that the DirectionsResult isn't plain JSON after all?
The API says:

"The directions response in JSON format retrieved from the directions
server."
(http://code.google.com/apis/maps/documentation/javascript/
reference.html#DirectionsResult)

So I assumed it was plain JSON I could serialise/deserialise using
json2.js. :-/

Thanks,
Stewart

Stewart

unread,
Aug 31, 2010, 6:51:50 AM8/31/10
to Google Maps JavaScript API v3
Ahhhh nuts.... I just answered my own question:

A DirectionsResult contains an array of DirectionsRoutes, and each
DirectionsRoute contains a overview_path property that is an array of
LatLng *objects*.

Those LatLng objects aren't JSON, so they're not handled by the
stringify and parse of the JSON library in json2.js. (As William
correctly pointed out.)

Bugger!

I'm off to explore options for property javascript *object*
serialisation. Any advice/tips/pointers are welcome!

Thanks all!

stu

unread,
Aug 31, 2010, 6:54:39 AM8/31/10
to Google Maps JavaScript API v3
Just a thought.
If you are only wanting to redraw the same route (ie not use any other
aspects of the DirectionsResult), you could pull the path points out
by iterating through the legs and steps.
This would give you an accurate path definition that you could then
store, and later retrieve, push back into a polyline MVCarray and
recreate the line exactly as the DirectionsResult described it at the
time you did the DirectionsService on it.
...stu

Stewart

unread,
Aug 31, 2010, 9:10:19 PM8/31/10
to Google Maps JavaScript API v3
If anyone is interested, I've taken the javascript serialisation
question over to here:
http://stackoverflow.com/questions/3608545/how-to-serialize-deserialize-javascript-objects

William

unread,
Sep 1, 2010, 12:51:46 AM9/1/10
to Google Maps JavaScript API v3
On Sep 1, 11:10 am, Stewart <stew...@bolidian.com> wrote:
> If anyone is interested, I've taken the javascript serialisation
> question over to here:http://stackoverflow.com/questions/3608545/how-to-serialize-deseriali...
>

I tried it out using the answers given at stackoverflow, here's a demo
page:
http://www.william-map.com/20100901/1/serial.htm

a problem for Version 3.2 of the Maps API is the minified property
"Ch" which represents the directions request.
This property isn't present in Version 3.1 or 3.0. I think it should
be given a documented name like "request".

directions response = {
"Ch":{"origin":"chicago, il","destination":"los angeles,
ca","travelMode":"DRIVING"},
"routes": {...},
"status": "OK"
}

...

Ben Appleton

unread,
Sep 1, 2010, 1:06:01 AM9/1/10
to google-map...@googlegroups.com
On Wed, Sep 1, 2010 at 2:51 PM, William <willia...@gmail.com> wrote:
> On Sep 1, 11:10 am, Stewart <stew...@bolidian.com> wrote:
>> If anyone is interested, I've taken the javascript serialisation
>> question over to here:http://stackoverflow.com/questions/3608545/how-to-serialize-deseriali...
>>
>
> I tried it out using the answers given at stackoverflow, here's a demo
> page:
> http://www.william-map.com/20100901/1/serial.htm
>
> a problem for Version 3.2 of the Maps API is the minified property
> "Ch" which represents the directions request.
> This property isn't present in Version 3.1 or 3.0.  I think it should
> be given a documented name like "request".

Why is that? This is used for internal bookkeeping.

> directions response = {
> "Ch":{"origin":"chicago, il","destination":"los angeles,
> ca","travelMode":"DRIVING"},
> "routes": {...},
> "status": "OK"
> }
>
> ...
>

> --
> You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
> To post to this group, send email to google-map...@googlegroups.com.
> To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.
>
>

William

unread,
Sep 1, 2010, 1:36:42 AM9/1/10
to Google Maps JavaScript API v3


On Sep 1, 3:06 pm, Ben Appleton <apple...@google.com> wrote:
>
> Why is that?  This is used for internal bookkeeping.
>
Oh I see, any property that is not documented here is not required to
be passed to DirectionsRenderers?
http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsResult

...

William

unread,
Sep 1, 2010, 2:13:30 AM9/1/10
to Google Maps JavaScript API v3
On Aug 31, 8:38 pm, Stewart <stew...@bolidian.com> wrote:
> So I assumed it was plain JSON I could serialise/deserialise using
> json2.js. :-/

modern browsers have native implementations of these functions, which
are used by json2.js if available, so I think it's preferable to
rename the functions to ensure you are using the stringify() and
parse() in json2.js

different browsers have slightly different native implementations of
these functions which don't appear to work in all cases with the
example.

for example, see the features introduced in FF 3.5.4
https://developer.mozilla.org/en/Using_JSON_in_Firefox

...

Ben Appleton

unread,
Sep 1, 2010, 2:16:26 AM9/1/10
to google-map...@googlegroups.com

Correct.

Stewart

unread,
Sep 2, 2010, 7:47:22 AM9/2/10
to Google Maps JavaScript API v3
I got this working by going through the deserialised-from-JSON
javascript object and manually calling the LatLng and LatLngBounds
constructors to fix the object. It works just fine.

@William I don't understand your point about json2.js. I'm using the
json2.js from json.org. I've tested my code in FF, IE and Safari
latest and it works fine.

On Sep 1, 4:16 pm, Ben Appleton <apple...@google.com> wrote:
> On Wed, Sep 1, 2010 at 3:36 PM, William <william.g...@gmail.com> wrote:
>
> > On Sep 1, 3:06 pm, Ben Appleton <apple...@google.com> wrote:
>
> >> Why is that?  This is used for internal bookkeeping.
>
> > Oh I see, any property that is not documented here is not required to
> > be passed to DirectionsRenderers?
> >http://code.google.com/apis/maps/documentation/javascript/reference.h...
Message has been deleted

sisdog

unread,
Dec 30, 2010, 4:46:26 PM12/30/10
to google-map...@googlegroups.com
Stewart,
I've tried to mimic your technique to save my DirectionsResponse to a database but it's not working.

Maybe you know what I'm missing?

thanks 

gully

unread,
Apr 20, 2011, 6:02:57 AM4/20/11
to google-map...@googlegroups.com
Thx all.

I am using this method in my app. And found the serialized data is much big than original json data from google DirectionsService. (usually 50x bigger).

and I changed code to use google.maps.geometry.encoding.encodePath/decodePath for the array of LatLng, it works and 5~8x bigger than original.
Any new idea?

******** CODE ********
custom_stringify = function(name, value){
if (/^(lat_lngs|path|overview_path)/.test(name)) {
return google.maps.geometry.encoding.encodePath(value);
} else if (value instanceof google.maps.LatLng){
return 'LL(' + value.lat() + ',' + value.lng() + ')';
} else if (value instanceof google.maps.LatLngBounds){
return 'LB(' +
value.getSouthWest().lat() + ',' + value.getSouthWest().lng() + ',' +
value.getNorthEast().lat() + ',' + value.getNorthEast().lng() + ')';
} else {
return value;
}
}

custom_parse = function(name, value){
if (/^(lat_lngs|path|overview_path)/.test(name)) {
return google.maps.geometry.encoding.decodePath(value);
} else if (/^LL\(/.test(value)){
var match = /LL\(([^,]+),([^,]+)\)/.exec(value);
return new google.maps.LatLng(match[1], match[2]);
} else if (/^LB\(/.test(value)){
var match = /LB\(([^,]+),([^,]+),([^,]+),([^,]+)\)/.exec(value);
return new google.maps.LatLngBounds(new google.maps.LatLng(match[1], match[2]),
new google.maps.LatLng(match[3], match[4]));
} else{
return value;
}
}

_S = function(obj){
return JSON.custom_stringify(obj, custom_stringify);
};
_US = function(str){
return JSON.custom_parse(str, custom_parse);
};
Reply all
Reply to author
Forward
0 new messages