Data Layer

The Google Maps Data layer provides a container for arbitrary geospatial data. You can use the Data layer to store your custom data, or to display GeoJSON data on a Google map.

  1. Overview
  2. Load GeoJSON
  3. Style GeoJSON Data
  4. Add Event Handlers
  5. Change Appearance Dynamically


Watch this DevBytes video to learn more about the Data Layer.

With the Google Maps JavaScript API you can mark up a map with a variety of overlays, such as markers, polylines, polygons, etc. Each of these annotations combines styling information with location data. The google.maps.Data class is a container for arbitrary geospatial data. Instead of adding these overlays, you can use the Data layer to add arbitrary geographical data to your map. If that data contains geometries, such as points, lines or polygons, the API will render these by default as markers, polylines and polygons. You can style these features as you would a normal overlay, or apply styling rules based on other properties contained in your data set.

The google.maps.Data class allows you to:

  • Add GeoJSON data to your map.
    GeoJSON is a standard for geospatial data on the internet. The Data class follows the structure of GeoJSON in its data representation and makes it easy to display GeoJSON data. Use the loadGeoJson() method to easily import GeoJSON data and display points, line-strings and polygons.
  • Use google.maps.Data to model arbitrary data.
    Most real-world entities have other properties associated with them. For example, stores have opening hours, roads have traffic speed, and each Girl Guide troupe has cookie selling turf. With google.maps.Data, you can model these properties, and style your data accordingly.
  • Choose how your data is represented, and change your mind on the fly.
    The Data layer lets you make decisions about the visualization and interaction of your data. For example, when looking at a map of convenience stores you might choose to display only those stores that sell transit tickets.

Load GeoJSON

GeoJSON is a common standard for sharing geospatial data on the internet. It is lightweight and easily human-readable, making it ideal for sharing and collaborating. With the Data layer, you can add GeoJSON data to a Google map in just one line of code.


Every map has a map.data object, which acts as a data layer for arbitrary geospatial data, including GeoJSON. You can load and display a GeoJSON file by calling the loadGeoJSON() method of the data object. The below example shows how to add a map and load external GeoJSON data.

var map;
function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 4,
    center: {lat: -28, lng: 137}

  // NOTE: This uses cross-domain XHR, and may not work on older browsers.

View Example

Sample GeoJSON

Most of the examples on this page use a common GeoJSON file. This file defines the six characters in ‘Google’ as polygons over Australia. Please feel free to copy or modify this file as you test out the Data layer.

Note: In order to load a json file from another domain, that domain must have enabled Cross-origin resource sharing.

The full text of the file can be seen below by expanding the small arrow next to the words google.json.

Style GeoJSON Data

Use the Data.setStyle() method to specify how your data should look. The setStyle() method takes either a StyleOptions object literal, or a function that computes the style for each feature.

Simple style rules

The simplest way to style features is to pass a StyleOptions object literal to setStyle(). This will set a single style for each feature in your collection. Note that each feature type is only able to render a subset of the available options. This means that its possible to combine styles for different feature types in a single object literal. For example, the below snippet sets both a custom icon, which only affects point geometries, and fillColor, which only affects polygons.

  icon: '//example.com/path/to/image.png',
  fillColor: 'green'

More information on valid style/feature combinations can be found in Style Options.

Below is an example of setting the stroke and fill color for several features using a StyleOptions object literal. Notice that each polygon is styled the same.

  // Set the stroke width, and fill color for each polygon
    fillColor: 'green',
    strokeWeight: 1

View example

Declarative style rules

If you want to update the style of a large number of overlays, such as markers or polylines, you typically have to iterate through each overlay on your map and set its style individually. With the Data layer, you can set rules declaratively and they will be applied across your entire data set. When either the data, or the rules, are updated, the styling will be automatically applied to every feature. You can use a features properties to customize its style.

For example, the below code sets the color of each character in our google.json by examining its position in the ascii character set. In this case we’ve encoded the character position along with our data.

// Color Capital letters blue, and lower case letters red.
// Capital letters are represented in ascii by values less than 91
map.data.setStyle(function(feature) {
    var ascii = feature.getProperty('ascii');
    var color = ascii > 91 ? 'red' : 'blue';
    return {
      fillColor: color,
      strokeWeight: 1

Remove styles

If you’d like to remove any applied styles, pass an empty object literal to the setStyles() method.

// Remove custom styles.

This will remove any custom styles that you’ve specified, and the features will render using the default styles. If you’d instead like to no longer render the features, set the visible property of StyleOptions to false.

// Hide the Data layer.
map.data.setStyle({visible: false});

Override default styles

Styling rules are typically applied to every feature in the Data layer. However, there are times when you’d like to apply special styling rules to specific features. For example, as a way to highlight a feature on click.

To apply special styling rules, use the overrideStyle() method. Any properties that you change with the overrideStyle() method are applied in addition to the global styles already specified in setStyle(). For example, the below code will change the fill color of a polygon on click, but will not set any other styles.

// Set the global styles.
  fillColor: 'green',
  strokeWeight: 3

// Set the fill color to red when the feature is clicked.
// Stroke weight remains 3.
map.data.addListener('click', function(event) {
   map.data.overrideStyle(event.feature, {fillColor: 'red'});

Call the revertStyles() method to remove all style overrides.

Style options

The options available for styling each feature depend upon the feature type. For example, fillColor will only render on polygon geometries, while icon will only appear on a point geometry. More information is available in the reference documentation for StyleOptions.

Available on all geometries

  • clickable: If true, the feature receives mouse and touch events
  • visible: If true, the feature is visible.
  • zIndex: All features are displayed on the map in order of their zIndex, with higher values displaying in front of features with lower values. Markers are always displayed in front of line-strings and polygons.

Available on point geometries

  • cursor: Mouse cursor to show on hover.
  • icon: Icon to show for the point geometry.
  • shape: Defines the image map used for hit detection.
  • title: Rollover text.

Available on line geometries

  • strokeColor: The stroke color. All CSS3 colors are supported except for extended named colors.
  • strokeOpacity: The stroke opacity between 0.0 and 1.0.
  • strokeWeight: The stroke width in pixels.

Available on polygon geometries

  • fillColor: The fill color. All CSS3 colors are supported except for extended named colors.
  • fillOpacity: The fill opacity between 0.0 and 1.0.
  • strokeColor: The stroke color. All CSS3 colors are supported except for extended named colors.
  • strokeOpacity: The stroke opacity between 0.0 and 1.0.
  • strokeWeight: The stroke width in pixels.

Add Event Handlers

Features respond to events, such as mouseup or mousedown. You can add event listeners to allow users to interact with the data on the map. In the below example, we add a mouseover event, that displays information about the feature under the mouse cursor.

  // Set mouseover event for each feature.
  map.data.addListener('mouseover', function(event) {
    document.getElementById('info-box').textContent =

Data layer events

The following events are common to all features, regardless of their geometry type:

  • addfeature
  • click
  • dblclick
  • mousedown
  • mouseout
  • mouseover
  • mouseup
  • removefeature
  • removeproperty
  • rightclick
  • setgeometry
  • setproperty

More information about these events can be found in the reference documentation for the google.maps.data class.

Change Appearance Dynamically

You can set the style of the Data layer by passing a function that computes the style of each feature to the google.maps.data.setStyle() method. This function will be called each time a feature’s properties are updated.

In the below example, we add an event listener for the click event that updates the feature’s isColorful property. The feature styling is updated to reflect the change as soon as the property is set.

  // Color each letter gray. Change the color when the isColorful property
  // is set to true.
  map.data.setStyle(function(feature) {
    var color = 'gray';
    if (feature.getProperty('isColorful')) {
      color = feature.getProperty('color');
    return /** @type {google.maps.Data.StyleOptions} */({
      fillColor: color,
      strokeColor: color,
      strokeWeight: 2

  // When the user clicks, set 'isColorful', changing the color of the letters.
  map.data.addListener('click', function(event) {
    event.feature.setProperty('isColorful', true);

  // When the user hovers, tempt them to click by outlining the letters.
  // Call revertStyle() to remove all overrides. This will use the style rules
  // defined in the function passed to setStyle()
  map.data.addListener('mouseover', function(event) {
    map.data.overrideStyle(event.feature, {strokeWeight: 8});

  map.data.addListener('mouseout', function(event) {

View example

Send feedback about...

Google Maps Javascript API
Google Maps Javascript API
Need help? Visit our support page.