API Key

There is one final step that needs to be added to the HTML page: an API key for GoogleMaps. This module will walk you through getting that key.

Getting an API key

GoogleMaps is an API provided by Google. There are many different APIs that Google offers, so if you're interested, feel free to play around with some of those later.

  1. If you haven't already, log into your Google Firebase account and create a project. The name doesn't matter; we're just using this project for the API Keys.

  2. Select your project from the list, then click `+ENABLE APIS AND SERVICES".

    Google Developer Console Home

  3. Search for Maps, then click on Google Maps Javascript API.

    GoogleMaps JS API

  4. Click Enable.

  5. In the side menu, click credentials, then click Create Credentials and select API Key.

    Get Google API Credentials

  6. Copy your API key and paste it in the apikeys.txt file.

  7. Add the following lines to the end of the body of the googlemaps.html file. Replace INSERT-YOUR-API-KEY-HERE with your key:

    <script type="text/javascript" src="https://maps.google.com/maps/api/js?key=INSERT-YOUR-API-KEY-HERE"></script>
    <script type="text/javascript" src="googlemaps.js"></script>
    <!DOCTYPE html>
    <html>
    <head>
     <meta charset="utf-8">
     <link href="googlemaps.css" rel="stylesheet">
     <title>Google Maps API</title>
    </head>
    <body>
     <h1>Maps Example</h1>
     <div id="map_canvas"></div>
     <script type="text/javascript" src="https://maps.google.com/maps/api/js?key=INSERT-YOUR-API-KEY-HERE"></script>
     <script type="text/javascript" src="googlemaps.js"></script>
    </body>
    </html>

IMPORTANT MAKE SURE YOU ADDED THE apikeys.txt FILE TO YOUR .gitignore! If you don't, all your API keys will be published to GitHub for anyone to use. Anytime you are using something for authentication or encryption purposes, make sure it's not accidentally pushed to GitHub.

Commit your work here as "GoogleMaps - API Key added".

This adds both the API Key and your googlemaps.js file to the html file. Next, we'll work on the JavaScript file.

Last updated