Parse Google Maps Geocode JSON Results

1. May 2009 15:29

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)

Tags: ,

Javascript




My Random Thought

I think the OCW is a great thing to have available to those who are in school, just finished school or just want to educate themself

http://ocwconsortium.org/

John On Twitter

Discounts