Simple Geocoding with Googe Maps

Categories: Development, Tutorials

What is Geocoding?

Geocoding is a process of transforming a description of a location into a pair of coordinates on the earth’s surface.

How does Geocoding work?

At a very basic level, your address input will be classified as relative, or absolute.

An example of relative data would be the house next door, where an absolute input would be a postal code or city, the more data provided the more accurate the results will be.

Why would I want to use Geocoding?

  • Presenting a list of addresses on a Map
  • Converting addresses into a format that can more easily be searched for distance
  • Visualize address data
  • You might want to Geocode photos so that they can be displayed on a map

How do I use Geocoding then?

Login to your Google Cloud Account

Then Enable the API by heading to the API’s Library. Next, search for and enable Geocoding API.

Next under your API & Services, click on Credentials, then Add a new API restriction and check Geocoding API.

NOTE: There could be a cost associated with this, Make sure to read and understand your Google cloud account and how their billing works.

If you don’t add the restriction you will likely see the following error.

{
   "error_message" : "This API project is not authorized to use this API. Please ensure that this API is activated in the APIs Console: Learn more: https://code.google.com/apis/console",
   "results" : [],
   "status" : "REQUEST_DENIED"
}

Using curl, or a tool like Postman, make a GET request to https://maps.googleapis.com/maps/api/geocode/json?address=90210&key={{key}}

Where 90210 would be your own zip/postal code or full address and that’s it, you should see something resembling the following results

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Beverly Hills",
               "short_name" : "Beverly Hills",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Los Angeles County",
               "short_name" : "Los Angeles County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "90210",
               "short_name" : "90210",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "338 N Canon Dr (2nd and 3rd Floor, Beverly Hills, CA 90210, USA",
         "geometry" : {
            "location" : {
               "lat" : 34.07018,
               "lng" : -118.399956
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 34.0715289802915,
                  "lng" : -118.3986070197085
               },
               "southwest" : {
                  "lat" : 34.0688310197085,
                  "lng" : -118.4013049802915
               }
            }
         },
         "place_id" : "ChIJ_SoTXge8woAR5pPtL6J1enM",
         "plus_code" : {
            "compound_code" : "3JC2+32 Beverly Hills, California, United States",
            "global_code" : "85633JC2+32"
         },
         "types" : [
            "beauty_salon",
            "establishment",
            "hair_care",
            "health",
            "point_of_interest"
         ]
      }
   ],
   "status" : "OK"
}

For more on Google’s Geocoding services check out their guide.

If you don’t happen to have an API key then this guide should get you on your way.


Adam Patterson

Adam Patterson

User Interface Designer & Developer with a background in UX. I have spent 5 years as a professionally certified bicycle mechanic and ride year-round.

I am a husband and father of two, I enjoy photography, music, movies, coffee, and good food.

You can find me on Twitter, Instagram, and YouTube!