Simple Googe Geolocation

What it is, and how you can use it.

Categories: Development, Tutorials

What is Geolocation?

Geolocation is a method used to find the location of your internet device on earth. Imagine you lost your cell phone, and use the find my phone option. The location of your phone will be your device’s last known location.

How does Geolocation work?

There are two main types of data collection.

The first is Device-Based

You have probably visited a website that prompted you for permission to access your location. This request has to be made from a secure site using SSL and if you are on a mobile device will require location services to be enabled.

The second is Server-Based.

Every request made to a server will include your device IP, that IP is typically stored in a Database where a physical location is matched. This isn’t always accurate as IP addresses are released and renewed frequently. Depending on your network, or your companies network these results can be very different.

Why would I want to use Geolocation?

  • Any time you have multiple locations like a food chain
  • E-commerce site where you have products across the country
  • You operate in multiple countries and want to redirect the customer to the proper website
  • Present visitors with relevant content

Geolocation is a quick and simple way to present information that is relevant to the customer and to make improvements to their websites.

How do I use Geolocation then?

Login to your Google Cloud Account

Then Enable the API by heading to the API’s Library. Next, search for and enable Geolocation 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.

Under your API & Services, click on Credentials, then Add a new API restriction and check Geolocation API.

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 POST request to https://www.googleapis.com/geolocation/v1/geolocate?key={{key}}

That’s it, you should see something resembling the following results

{
  "location": {
    "lat": 53.595340799999995,
    "lng": -113.5706112
  },
  "accuracy": 2435
}

For more on Google’s Geolocation 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!