Parse Google Maps Geocode JSON Results

1. May 2009

I'm knew to the whole JSON concept and playing with the Google Maps API helped me understand it a bit better. However, I didn't like Google's example too much on how to use the JSON results that you get when you go a Geocode on a given address.  I have put some code on here as to the basic concept of how you get and use the JSON result returned from the google maps api geocode.  I also have attached the file that include the entire javascript code that places the markers on the map given an array of zip codes.  Anyway, this is a start for those out there first using google maps api for JSON. 

NOTE: You will need this file from json.org to help load the JSON results into an object. 

 

//make a geocode call via the GDownloadUrl because Google advises not to
                //use the GClientGeocoder in a loop. We'll return the response as JSON
               //then add markers on the map.
                var j;
                var cnt = -1;
                for (j = 0; j < zipCode.length; j++) {

                    //response of JSON results can be found here:
                    //http://code.google.com/apis/maps/documentation/geocoding/index.html#GeocodingResponses
                    GDownloadUrl("http://maps.google.com/maps/geo?q=" + zipCode[j] + "&output=json&oe=utf8&sensor=false&key=yourKey", function(data, responseCode) {
                        cnt++;
                        //alert(data);
                        var streetName = "";
                        var city = "";
                        var state = "";
                        var zipCode = "";
                        var point = null;

                        jsonResults = JSON.parse(data); //load JSON response
                       
                        //we do a for-loop here because if the address is generic enough it may bring back more than
                        //one result. which in the JSON return would be multiple Placemarks.
                        var i;
                        for (i = 0; i < jsonResults.Placemark.length; i++) {

                            xCoord = jsonResults.Placemark[i].Point.coordinates[0];
                            yCoord = jsonResults.Placemark[i].Point.coordinates[1];

                            //depending on the given address we may not have Locality and we may not have Thoroughfare
                            if (jsonResults.Placemark[i].AddressDetails.Country.AdministrativeArea.Locality != null) {

                                //check for Thoroughfare
                                if (jsonResults.Placemark[i].AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare != null) {
                                    streetName = jsonResults.Placemark[i].AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
                                }

                                city = jsonResults.Placemark[i].AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
                                zipCode = jsonResults.Placemark[i].AddressDetails.Country.AdministrativeArea.Locality.PostalCode.PostalCodeNumber;
                            }

                            state = jsonResults.Placemark[i].AddressDetails.Country.AdministrativeArea.AdministrativeAreaName;

                            point = new GLatLng(yCoord, xCoord);
                            map.addOverlay(createMarker(point, cnt, streetName, city, state, zipCode));


                        } //end of for-loop for jsonResults.Placemark

                        //set center of map is the last placemark in the JSON response
                        map.setCenter(point, 11);
                       
                    });  //end of GDownloadUrl()

                } //end of for-loop

Again, I have attached a txt file with all the javscript to make this happen.  

Here are some helpful links as well:

http://code.google.com/apis/maps/documentation/services.html#Geocoding_Structured
http://code.google.com/apis/maps/documentation/geocoding/index.html

 

jsonExampleGoogleMapsAPI.txt (5.28 kb)

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Javascript ,

Comments

Benny
10/13/2009 3:50:03 PM #
Hi John,

Thanks for the useful post.  I am trying to build a vb.net application (web or desktop) to query address using google maps by supplying business name and state. I would basically want to supply these two parameters and fetch the first result returned to save it to a text file or database.

I did some research online but wasn't able to find directly related samples. Do you have any suggestion on how I can approach this problem?
10/13/2009 4:36:33 PM #
Hey Benny, well off the top of my head, I don't think this post will help with what you are trying to do since you would like to get it done in VB.NET.  What I would recommend doing is looking at the following:

The WebClient class in .NET

msdn.microsoft.com/.../system.net.webclient%28VS.80%29.aspx

the DownloadString method may be your best bet. At this point you don't need it to be JSON. If I remember right you can request XML as well, just JSON is a smaller byte return.  If you do choose JSON at this point you can use JSON Serialization within .NET 3.5. Take a look at this link:

pietschsoft.com/.../...ContractJsonSerializer.aspx

Again I would start with the WebClient thought since I know google offers a couple different returns and see which fits best.

John M
Benny
10/13/2009 7:05:23 PM #
Hi John,

Thanks for the quick reply.  I did some more research and the problem was actually simpler than I thought.  I used a WebClient to read the page and parse the first address returned in the page using the code below and it seems to work well.

Thanks again!!!

Dim client As New System.Net.WebClient()
Dim page As String = client.DownloadString("http://maps.google.com/maps?q=" & reqStr)

Dim beginPos As Integer = page.IndexOf("laddr:")
'parse address
If beginPos > 0 Then
    Dim str = page.Substring(beginPos + 7)
    Dim stopPos As Integer = str.IndexOf("""")
    addr = str.Substring(0, stopPos)
Else
    addr = ""
End If
3/20/2010 1:52:40 AM #
Completely understand what your stance in this matter. Although I would disagree on some of the finer details, I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading