Geolocations using Google API

To create this simple web page, one will just need two files: a web page to utilize the Google API and a JavaScript file containing coordinates that will be passed to the Google API.

  1. One will first need to obtain an API key to utilize Google’s APIs. To obtain a key, go to Google’s article: Setting up API Keyshttps://support.google.com/googleapi/answer/6158862?hl=en
  2. Once you have an API Key, create an HTML page called geolocation.html.
  3. You will need to specify your API Key as part of the Google script:

    <script src=”https://maps.googleapis.com/maps/api/js?key=<your API Key>”></script>

    Below is an example of an html page utilizing the above:

4. As can one can see in the above html page, a JavaScript file called geolocation.js is used. The JavaScript file allows us to ask Google for our current coordinates as well as specify locations of businesses of coordinates we want shown on the map.

5. As you can see in the screenshot below, the visitor’s coordinates (longitude and latitude) is provided from the Google API. The coordinates in red is based on the list of offices from a JSON formatted list in the JavaScript.