Gibbs
Gibbs

Reputation: 22956

Matching city and state using Google API's?

I need to check whether the given combination of city and state are correct using Google API.

Ways I found so far

1. USING GeoCoding API:

We can a make a call with zipCode which ll return a JSON. There is a field describing formatted address which contains city,state,zipcode,area,etc.

http://maps.googleapis.com/maps/api/geocode/json?address=ZIPCODE&sensor=true

2. ZIPtastic API

Similar to the above but it returns small amount of information.

But now i want to do without ZIPCode. [i.e] City and state are given, i need to check whether the given combinations are correct using API.Are there any ways? Please suggest some ideas.

EDIT: I need to check whether given city,state,country,zipcode are correct.

http://maps.googleapis.com/maps/api/geocode/json?address=NY&address=10001&sensor=true&components=country:US

The above one returns same result as

http://maps.googleapis.com/maps/api/geocode/json?address=NY&address=10001&sensor=true&address=albanycomponents=country:US

Zipcode is 10001, State is NY, City is Albany, Country is US. Why does it return the same result. Am i getting the correct result or doing something?

Upvotes: 0

Views: 2557

Answers (2)

Gan
Gan

Reputation: 945

Wrote a function in angular to get the address.

public formatAddress(googleAddr: any): any {


    let properAddress: any = {
      latitude: '',
      longitude: '',
      address: '',
      premises: '',
      area: '',
      street: '',
      locality: '',
      state: '',
      country: '',
      timezone: '',
      pincode: ''
    };

    let acutalPremise = '';

    if (googleAddr && googleAddr.address_components) {

      googleAddr.address_components.forEach((addr:any) => {

        // country
        if (addr.types.indexOf('country') != -1) {

          properAddress.country = addr.long_name;
        }

        // state
        if (addr.types.indexOf('administrative_area_level_1') != -1) {

          properAddress.state = addr.long_name;
        }

        // Area
        if (addr.types.indexOf('sublocality_level_1') != -1) {

          properAddress.area = addr.long_name;
        }

        // locality
        if (addr.types.indexOf('locality') != -1) {

          properAddress.locality = addr.long_name;
        }

        // street
        if (addr.types.indexOf('route') != -1) {

          properAddress.street = addr.long_name;
        }
        // acutal address premises

        if (addr.types.indexOf('premise') != -1) {

          acutalPremise = addr.long_name;
        }

        // postal code
        if (addr.types.indexOf('postal_code') != -1) {

          properAddress.pincode = addr.long_name;
        }
      });
    }

    // get the timezone
    properAddress.latitude = googleAddr.geometry.location.lat();
    properAddress.longitude = googleAddr.geometry.location.lng();
    properAddress.address = googleAddr.name + ', ' + googleAddr.formatted_address;
    properAddress.premises = properAddress.street || acutalPremise ? googleAddr.name : '';


    return properAddress;
  }

Upvotes: 0

Nicolas R
Nicolas R

Reputation: 14589

You can use what you want in the address parameter of the GeoCoding API, for example:

http://maps.googleapis.com/maps/api/geocode/json?address=Paris&sensor=true

Here I got the results matching the address Paris: Paris in France, Paris in Texas, etc.

So you can use it to input your city and check if the state is valid. For this purpose, limit your search to the US also, using this complement: components=country:US

Sample: http://maps.googleapis.com/maps/api/geocode/json?address=Paris&sensor=true&components=country:US

Result:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Comté de Lamar",
               "short_name" : "Comté de Lamar",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Texas",
               "short_name" : "TX",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "États-Unis",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Paris, Texas, États-Unis",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 33.7383781,
                  "lng" : -95.435455
               },
               "southwest" : {
                  "lat" : 33.6118529,
                  "lng" : -95.62792789999999
               }
            },
            "location" : {
               "lat" : 33.6609389,
               "lng" : -95.55551299999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 33.7383781,
                  "lng" : -95.435455
               },
               "southwest" : {
                  "lat" : 33.6118529,
                  "lng" : -95.62792789999999
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Comté de Henry",
               "short_name" : "Comté de Henry",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Tennessee",
               "short_name" : "TN",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "États-Unis",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "38242",
               "short_name" : "38242",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Paris, Tennessee 38242, États-Unis",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 36.3291321,
                  "lng" : -88.2650759
               },
               "southwest" : {
                  "lat" : 36.266,
                  "lng" : -88.36711489999999
               }
            },
            "location" : {
               "lat" : 36.3020023,
               "lng" : -88.32671069999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 36.3291321,
                  "lng" : -88.2650759
               },
               "southwest" : {
                  "lat" : 36.266,
                  "lng" : -88.36711489999999
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Comté d'Edgar",
               "short_name" : "Comté d'Edgar",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Illinois",
               "short_name" : "IL",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "États-Unis",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "61944",
               "short_name" : "61944",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Paris, Illinois 61944, États-Unis",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 39.6485756,
                  "lng" : -87.6505408
               },
               "southwest" : {
                  "lat" : 39.581415,
                  "lng" : -87.721046
               }
            },
            "location" : {
               "lat" : 39.611146,
               "lng" : -87.6961374
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 39.6485756,
                  "lng" : -87.6505408
               },
               "southwest" : {
                  "lat" : 39.581415,
                  "lng" : -87.721046
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Comté de Bourbon",
               "short_name" : "Comté de Bourbon",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Kentucky",
               "short_name" : "KY",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "États-Unis",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "40361",
               "short_name" : "40361",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Paris, Kentucky 40361, États-Unis",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 38.238271,
                  "lng" : -84.232089
               },
               "southwest" : {
                  "lat" : 38.164922,
                  "lng" : -84.3073259
               }
            },
            "location" : {
               "lat" : 38.2097987,
               "lng" : -84.2529869
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 38.238271,
                  "lng" : -84.232089
               },
               "southwest" : {
                  "lat" : 38.164922,
                  "lng" : -84.3073259
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Comté d'Oxford",
               "short_name" : "Comté d'Oxford",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Maine",
               "short_name" : "ME",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "États-Unis",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Paris, Maine, États-Unis",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 44.31228309999999,
                  "lng" : -70.4148551
               },
               "southwest" : {
                  "lat" : 44.1753699,
                  "lng" : -70.56617199999999
               }
            },
            "location" : {
               "lat" : 44.2597917,
               "lng" : -70.5006152
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 44.31228309999999,
                  "lng" : -70.4148551
               },
               "southwest" : {
                  "lat" : 44.1753699,
                  "lng" : -70.56617199999999
               }
            }
         },
         "types" : [ "locality", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Paris",
               "short_name" : "Paris",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Short Mountain",
               "short_name" : "Short Mountain",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Comté de Logan",
               "short_name" : "Comté de Logan",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Arkansas",
               "short_name" : "AR",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "États-Unis",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "72855",
               "short_name" : "72855",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Paris, Arkansas 72855, États-Unis",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 35.3064849,
                  "lng" : -93.67508309999999
               },
               "southwest" : {
                  "lat" : 35.2672499,
                  "lng" : -93.7618069
               }
            },
            "location" : {
               "lat" : 35.2920325,
               "lng" : -93.7299173
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 35.3064849,
                  "lng" : -93.67508309999999
               },
               "southwest" : {
                  "lat" : 35.2672499,
                  "lng" : -93.7618069
               }
            }
         },
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

Then you parse the JSON and are able to find what you want, with the administrative_area_level_1 field here for the state!

Edit: in addition, you can also specify that your search is only on the locality field:

http://maps.googleapis.com/maps/api/geocode/json?sensor=true&components=country:US|locality:Paris to filter possible results coming from the name Paris in other fileds

Upvotes: 2

Related Questions