Skip to content

Update Google Maps Static API integration to require and configure an API key #11

@jimmckeeth

Description

@jimmckeeth

Overview

The training code integrates the Google Maps Static API to display a map image for a log entry's GPS coordinates. The current code has an empty GOOGLE_MAPS_STATIC_API_KEY constant. Since June 2018, Google has required a valid billing-enabled API key for all Maps API requests — unauthenticated requests return an error image. The training must be updated to document how to obtain a key and configure it safely.

Background

What Changed in 2018

Google's Maps Platform pricing change (June 2018) made API keys mandatory and introduced per-request billing. Requests without a valid key return a watermarked "For development purposes only" image and are rate-limited to near zero.

Current Code

const
  GOOGLE_MAPS_STATIC_API_KEY = '';  // <-- empty, broken

procedure LoadMapImage;
var
  URL: string;
begin
  URL := Format('https://maps.googleapis.com/maps/api/staticmap?center=%g,%g&zoom=15&scale=2&size=600x300&key=%s',
    [Lat, Lon, GOOGLE_MAPS_STATIC_API_KEY]);
  // HTTP GET to load image...
end;

Recommended Approach for Training

  1. Document the steps to create a Google Cloud project, enable the Maps Static API, and create an API key restricted to the Maps Static API.
  2. Store the key in a separate configuration file (config.pas or a text resource) that is excluded from source control via .gitignore — teaching good secrets hygiene.
  3. Provide a fallback path if no key is configured: display a placeholder image and a message directing the student to configure a key, rather than making a failing HTTP request.
  4. Alternatively, offer OpenStreetMap Static Maps (via staticmap.openstreetmap.de or Maps for Free) as a no-key fallback for the training exercise.

Files Affected

lab-src/Lab*/units/  (wherever GOOGLE_MAPS_STATIC_API_KEY is defined)
lab-src/Lab*/forms/formMain.pas  (map image load call)
lab-src/Lab*/frames/uEntryDetailsFrame.pas  (if map is displayed there)
New file: lab-src/Lab*/units/config.pas (API key configuration, git-ignored)
.gitignore (add config.pas or key file)

Steps to Address

  1. Move GOOGLE_MAPS_STATIC_API_KEY out of the main source into a separate config.pas file:
    unit config;
    interface
    const
      GOOGLE_MAPS_STATIC_API_KEY = '';  // Fill in your key
    implementation
    end.
  2. Add config.pas to .gitignore and provide a config.pas.template in its place.
  3. Add a check before the API call:
    if GOOGLE_MAPS_STATIC_API_KEY = '' then
    begin
      imgMap.Visible := False;
      lblMapNote.Text := 'Configure Google Maps API key in config.pas to see map';
      Exit;
    end;
  4. Update lab instructions with step-by-step instructions to get a Maps Static API key from Google Cloud Console.
  5. Optionally add an OpenStreetMap tile-based fallback for students who do not want to set up billing.

Test Plan

  • With an empty key: the map image area is hidden and the configuration message is shown — no failed HTTP request is made.
  • With a valid Maps Static API key: a map image centered on the log entry coordinates loads correctly on Android and iOS.
  • config.pas is listed in .gitignore and does not appear in git status output after being created.
  • config.pas.template exists in the repo with an empty key and instructions comment.
  • The map image loads within 5 seconds on a normal Wi-Fi connection.
  • A missing or revoked API key returns the error image gracefully (not a crash).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions