Google 2D Map Tiles are simply the division of the world into an indexed grid. It lets you access and use map data efficiently and dynamically at multiple cartographic scales. The Map Tiles API gives you access to multiple thematic geo-datasets, including Google-curated roadmap image tiles and satellite imagery.

Visualize Google 2D Tiles

To show Google 2D Tiles on a LuciadRIA map, you must access the Google Map Tiles API, create a model for the Google 2D Tiles data, and visualize that model in a map layer. You also need to visualize the correct attributions.

google2DTiles LeuvenOffice
Figure 1. A LuciadRIA view of the Leuven Hexagon office in Google 2D Tiles.

Get a Google Maps API key

To start working with Google 2D Tiles, you must get an API key from Google.

Note that more terms may apply for deployment. For more information about Google licensing, see the Google Map Tiles API Policies.

Visualize Google Maps data

Visualizing Google Maps data requires two steps:

  1. Create a GoogleMapsTileSetModel with your API key and an optional set of map requirements.

  2. Use a RasterTileSetLayer to visualize the model.

Expand for Program: Creating a Google Maps raster layer:

Program: Creating a Google Maps raster layer.
async function createGoogleMapsLayer(): Promise<RasterTileSetLayer> {
  const model = await createGoogleMapsTileSetModel(GOOGLE_MAPS_API_KEY, {
    mapType: "roadmap",
    language: "en-US",
    region: "US"
  });
  return new RasterTileSetLayer(model, {
    label: "Google Maps",
    layerType: LayerType.BASE,
    id: "Google"
  });
}

Configure display option with Google Maps Sessions

The Google Maps Tile API offers an elaborate set of map display options to tune the styling of the map. A display option configuration is captured in a Google Maps session. Such a session is a session token that captures all display options in a UUID. This session token is sent as part of the tile request URL. For more information about session tokens and display options, please refer to Google Map Tiles session tokens.

createGoogleMapsTileSetModel creates a GoogleMapsSession object behind the scenes. This object contains the session token as well as a number of other properties that are needed to create a GoogleMapsTileSetModel.

Optimize your application with Google Maps Sessions

Session tokens are valid for a longer time. At the time of writing, a session is valid for 2 weeks. It is advisable that you reuse sessions when possible, for example if you always use the same display options. Because the session ID is present in tile request URLs this means that you can benefit from caching if you reuse session IDs, even when you reload your web application.

Program: Creating a Google Maps raster layer from a Google Session shows you how you can leverage the browser’s local storage facilities to store Google Maps sessions and reuse them.

Expand for Program: Creating a Google Maps raster layer from a Google Session:

Program: Creating a Google Maps raster layer from a Google Session.
  const LOCAL_STORAGE_KEY = "GoogleSession";
  let reusableSession: GoogleMapsSession | null = null;

  // try to retrieve a session from local storage
  const storedSession = localStorage.getItem(LOCAL_STORAGE_KEY);

  // check session validity
  if (storedSession !== null) {
    reusableSession = JSON.parse(storedSession) as GoogleMapsSession;
    const expirationDate = parseInt(reusableSession.expiry, 10) * 1000;
    // Don't use the session if it expires within 30 minutes
    if (expirationDate < (Date.now() + 30 * 60 * 1000)) {
      reusableSession = null;
    }
  }

  // if we don't have a valid session at this point: create one and store it in local storage
  if (!reusableSession) {
    reusableSession = await createGoogleMapsSession(GOOGLE_MAPS_API_KEY, {
      mapType: "satellite",
      layerTypes: ["layerRoadmap"]
    });
    localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(reusableSession));
  }

  const model = createGoogleMapsTileSetModelFromSession(reusableSession);
  return new RasterTileSetLayer(model, {
    label: "Google",
    layerType: LayerType.BASE
  });
}

Add the Google attributions

Google requires you to show the Google logo and the correct attributions as an overlay on the map.

To set the correct logo, override the getLogo method of your GoogleMapsTileSetModel. Program: Add the Google logo shows how you can do that.

Expand for Program: Add the Google logo:

Overlay tileset attributions

The attributions for Google 2D Tiles vary depending on the tiles that you have in your view. Different zoom levels and different zones on the globe have their own copyright information. The Google Map Tiles API policies specify that you must collect, sort, and aggregate all that information. LuciadRIA does that for you automatically.

Get the logo and attributions on screen

The LuciadRIA sample code contains an Attribution overlay component to handle this.

Please refer to the sample samples/common/ui/overlay/Attribution.tsx for more details.

Expand for Program: Google Maps attributions and logo: