Problem with JSON and the latlng object

503 views
Skip to first unread message

Sheepz

unread,
Mar 24, 2010, 5:00:22 PM3/24/10
to Google Maps JavaScript API v3
Hi All, I'm using Json to parse the gecode result and pass it to my
java code, where I rebuild it.
This it my JS code for parsing the result to a string:
var myJSONText = JSON.stringify(results[0], null);
it returns the following for a query about Germany:
"[
{
"types":["country","political"],
"formatted_address":"Deutschland",
"address_components":
[{"long_name":"Deutschland","short_name":"DE","types":
["country","political"]}],
"geometry":{
"location":{"b":51.165691,"c":10.451526},
"location_type":"APPROXIMATE",
"viewport":{"c":{"b":47.4430843,"c":54.6103698},"b":{"d":180,"c":
2.255725,"b":18.647327}},
"bounds":{"c":{"b":47.270127,"c":55.0815},"b":{"d":180,"c":
5.8663566,"b":15.0418536}}}
}
]"

Note the location, viewport and bounds.
These are not proper json objects and are therefore parsed in a way
that cannot be used to reconstruct the object on the java side.
The objects in question are the LatLng and the LatLngBounds objects.
Is this a bug that's going to be fixed soon?
Am i missing something in the way i parse it?

Ben Appleton

unread,
Mar 24, 2010, 5:12:09 PM3/24/10
to google-map...@googlegroups.com

This is working as intended.


--
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.

Sheepz

unread,
Mar 24, 2010, 5:29:18 PM3/24/10
to Google Maps JavaScript API v3
Thanks for the answer but if possible, I'd appreciate some more
details, I understand that this is not something you consider as a
defect, can you please elaborate on why you say that? is there a way
to stringify this correctly and I'm just not using it? my workaround
at the moment is some search and replace method that runs on the
object on the server side but it seems like something is being done
wrong, either on my end or in the implementation, so please elaborate
in your response.
Tia,
Elad.

On Mar 24, 5:12 pm, Ben Appleton <apple...@google.com> wrote:
> This is working as intended.
>

> google-maps-js-a...@googlegroups.com<google-maps-js-api-v3%2Bunsu...@googlegroups.com>

Ben Appleton

unread,
Mar 24, 2010, 6:25:42 PM3/24/10
to google-map...@googlegroups.com
To be sure, you mean the JavaScript Geocoder (http://code.google.com/apis/maps/documentation/v3/services.html#Geocoding) not the HTTP Web Service (http://code.google.com/apis/maps/documentation/geocoding/index.html) right?

The JavaScript Geocoder does not return JSON, it returns a JavaScript Object.  For convenience we provide google.maps.LatLng and google.maps.LatLngBounds instances where appropriate, not eg. {lat:..., lng:...} structs.

What is your Java code - are you using GWT or are you passing the geocode result to a Java server?

- Ben
> To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.

Sheepz

unread,
Mar 24, 2010, 6:49:32 PM3/24/10
to Google Maps JavaScript API v3
I'm using the Javascript Geocoder.
However, according to the documentation found at:
http://code.google.com/apis/maps/documentation/v3/services.html#GeocodingResponses
: "The GeocoderResults object literal is a JSON object ..."
My java code is fairly simple for example's sake, it is:

JAVA SIDE:
~~~~~~~~~
private void handleCallback(String result, String status,
AjaxRequestTarget target) {

JSONDeserializer<GeocodeResult> deser =new
JSONDeserializer<GeocodeResult>();
deser.use( null, GeocodeResult.class );


String fixedResult = fixResult(result);
System.out.println("fixedResult=["+fixedResult+"]");
GeocodeResult resultObject =
deser.deserialize( fixedResult );
System.out.println("at handleCallBack with result: ["+result
+"] and status:["+status+"] and result object: ["+resultObject+"]");
}

private String fixResult(String result) {
return result.replaceAll("(location.*?)\"b\"(.*?)\"c\"",
"$1\"lat\"$2\"lon\"");
}

HTML SIDE:
~~~~~~~~~
function handleResponse(results, status){
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});


var myJSONText = JSON.stringify(results[0], null);

wicketCallBack(myJSONText,status);
} else {
alert("Geocode was not successful for the
following reason: " + status);
}
}

NOTES:
~~~~~~
I'm using ajax for moving the data around, my web framework is wicket
(not GWT), running on tomcat with liferay portal, flexJson on the
server side, and json2.js (from jquery) on the client side.
If there's any other data that you need, I'll be happy to provide it!

I understand that you do not provide the json structure for the latlng
and the latlngbounds object, which is kind of the reason for the post
- why not? I'm sure I'm not the only one that's doing this - moving
data from javascript to java and back is kinda the point of json so
the logic of doing it half way eludes me...

e.

On Mar 24, 6:25 pm, Ben Appleton <apple...@google.com> wrote:
> To be sure, you mean the JavaScript Geocoder (http://code.google.com/apis/maps/documentation/v3/services.html#Geoco...)

> <google-maps-js-api-v3%2Bunsu...@googlegroups.com<google-maps-js-api-v3%252Buns...@googlegroups.com>

Ben Appleton

unread,
Mar 24, 2010, 8:11:07 PM3/24/10
to google-map...@googlegroups.com
On Thu, Mar 25, 2010 at 9:49 AM, Sheepz <elad...@gmail.com> wrote:
I'm using the Javascript Geocoder.
However, according to the documentation found at:
http://code.google.com/apis/maps/documentation/v3/services.html#GeocodingResponses
: "The GeocoderResults object literal is a JSON object ..."

I've asked our docs guy to fix that.  The authoritative reference is here:
The JS geocoder is only intended for use by JS.  Have you considered using the web service from your server?
 
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.

Sheepz

unread,
Mar 24, 2010, 8:27:37 PM3/24/10
to Google Maps JavaScript API v3
Thanks for the reply.
I did consider using the server to perform the call, however, to
reduce load on the servers, I decided to do a client side call, are
you suggesting that we should not do it this way?
Also, I think that I must reiterate my point since I really do love
using google products, and since the maps API is still in 'labs', I
urge you to reconsider using the a jsonised version of the LatLng and
BoundsLatLng objects since it will be consistent with the rest of the
v3 api and will make life much simpler for people like me in the
future.
Thanks,
e.

On Mar 24, 8:11 pm, Ben Appleton <apple...@google.com> wrote:


> On Thu, Mar 25, 2010 at 9:49 AM, Sheepz <eladk...@gmail.com> wrote:
> > I'm using the Javascript Geocoder.
> > However, according to the documentation found at:
>

> >http://code.google.com/apis/maps/documentation/v3/services.html#Geoco...


> > : "The GeocoderResults object literal is a JSON object ..."
>

> I've asked our docs guy to fix that.  The authoritative reference is here:http://code.google.com/apis/maps/documentation/v3/reference.html#Geoc...

> > > <google-maps-js-api-v3%2Bunsu...@googlegroups.com<google-maps-js-api-v3%252Buns...@googlegroups.com>
> > <google-maps-js-api-v3%252Buns...@googlegroups.com<google-maps-js-api-v3%25252Bun...@googlegroups.com>

Ben Appleton

unread,
Mar 24, 2010, 8:58:16 PM3/24/10
to google-map...@googlegroups.com
On Thu, Mar 25, 2010 at 11:27 AM, Sheepz <elad...@gmail.com> wrote:
Thanks for the reply.
I did consider using the server to perform the call, however, to
reduce load on the servers, I decided to do a client side call, are
you suggesting that we should not do it this way?

This is not an intended use of the JS API, so I don't recommend this.  As you've found, the JS service produces JS Objects not JSON.

Are you concerned for the load on your servers or ours?

Also, I think that I must reiterate my point since I really do love
using google products, and since the maps API is still in 'labs', I
urge you to reconsider using the a jsonised version of the LatLng and
BoundsLatLng objects since it will be consistent with the rest of the
v3 api and will make life much simpler for people like me in the
future.

The v3 API consistently promotes {lat: ..., lng:...} structures to google.maps.LatLng objects (and likewise for LatLngBounds).  JS developers find this much more convenient than having to promote these structures for themselves.

Cheers
Ben

To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.

Sheepz

unread,
Mar 25, 2010, 11:12:50 AM3/25/10
to Google Maps JavaScript API v3
It might not be the intended use, but it recommended by Google in this
post by Mano Marks:
http://randommarkers.blogspot.com/2010/03/client-side-geocoding-rocks.html
The load that I'm concerned about is on our servers and our more to
the point, I'm concerned about the quota running out.
about the second comment, I'm sorry but i don't understand, you're
saying that the V3 API 'promotes' the json objects to be JS objects?
what for? why not keep it as json and if need be, the js developers
can 'promote' it on their own.
The bottom line, and what I'm trying to say is that what the api is
lacking at the moment is consistency, it seems to me that you
sacrificed this consistency for comfort, which is a decision you made,
and if that is the case, then all i can do is make my objections heard
and del with it on my code, however, if this was done because of an
omission or lack of foresight regarding my use case, then i urge you
to fix it before v3 comes out of beta\labs and becomes final.
Thanks,
e.
On Mar 24, 8:58 pm, Ben Appleton <apple...@google.com> wrote:
> > > > > <google-maps-js-api-v3%2Bunsu...@googlegroups.com<google-maps-js-api-v3%252Buns...@googlegroups.com>
> > <google-maps-js-api-v3%252Buns...@googlegroups.com<google-maps-js-api-v3%25252Bun...@googlegroups.com>
>
> > > > <google-maps-js-api-v3%252Buns...@googlegroups.com<google-maps-js-api-v3%25252Bun...@googlegroups.com>
> > <google-maps-js-api-v3%25252Bun...@googlegroups.com<google-maps-js-api-v3%2525252Bu...@googlegroups.com>
> ...
>
> read more »

Chad Killingsworth

unread,
Mar 25, 2010, 2:24:41 PM3/25/10
to Google Maps JavaScript API v3
> about the second comment, I'm sorry but i don't understand, you're
> saying that the V3 API 'promotes' the json objects to be JS objects?
> what for? why not keep it as json and if need be, the js developers
> can 'promote' it on their own.

Because almost every use for this requires an actual JS object. Why
force the majority of developers to create a LatLng object?

I believe your root issue is that the LatLng object serializes to
something you can't use. Instead of using JSON.stringify, why not
create your own function to walk through the returned data and create
a proper JSON string. The function wouldn't even be that complex.

Chad Killingsworth

Sheepz

unread,
Mar 25, 2010, 3:02:27 PM3/25/10
to Google Maps JavaScript API v3
I did eventually do that, and I understand what you're saying about
the majority of user and accept it, however my point from the begining
was that the problem is that we have two methods together here, if
users usually prefer js object, then why not make the entire structure
a js object? Perhaps there isn't a right answer here and maybe it's
just me, but I feel that when a response object is received it should
be either all js or all json, the way it is now imho just makes it
harder for people like me who prefer it jsoniez and for people who
prefer it jsed...

I appreciate the responses and their promptness, keep up the good
work!
e.

On Mar 25, 2:24 pm, Chad Killingsworth

Mano Marks

unread,
Apr 2, 2010, 2:42:48 PM4/2/10
to Google Maps JavaScript API v3
Hi,

Sorry for taking a bit to respond. There's actually no contradiction
between what I wrote in my blog post and what Ben wrote. The JS
geocoder is meant primarily for use in the JS API. So we wrote it in
such a way that it would be most efficient for that purpose, and give
you the best performance. To create an additional JSON output would
bloat the code a bit.

That said, there's nothing wrong with taking some of the properties
you get and sending them to the server for processing. It's not the
primary focus of the JS geocoding API, but it is a use case we
support.

Mano

Reply all
Reply to author
Forward
0 new messages