Google Maps Engine API

Hello, World

Overview

The Google Maps Engine API is accessed using HTTP requests to a base URL combined with operation-specific paths and parameters.

Data that is made public in the Maps Engine product is accessible by the API without authentication; only a key is required. The examples on this page use publicly-accessible data.

https://www.googleapis.com/mapsengine/v1/tables/12421761926155747447-06672618218968397709/features
  &maxResults=500
  &snapshot=published
  &key=AIzaSyAzgdOiFagRujMB4kr-vyRTbPw3V6d1bWw

You can send the sample request above from your browser: try it.

cURL

cURL is a command-line tool that can be used to make HTTP requests. You can use it to make requests against the Google Maps Engine API as follows:

$ curl "https://www.googleapis.com/mapsengine/v1/tables/12421761926155747447-06672618218968397709/features?maxResults=500&version=published&key=YOUR_API_KEY"

The response is a JSON object containing the set of features from the specified table.

JavaScript

To make an HTTP request with JavaScript, you can use the jQuery JavaScript library. jQuery's ajax method performs an asynchronous HTTP GET request to a specified URL.

The following code retrieves the features from the specified public table and writes them to the JavaScript console. Note that jQuery.ajax must be called from a web server; it won't work if you try to run this from a file: location.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script>
<script>

  function getFeatures() {
    jQuery.ajax({
      url: 'https://www.googleapis.com/mapsengine/v1/tables/12421761926155747447-06672618218968397709/features?maxResults=500&version=published&key=YOUR_API_KEY',
      dataType: 'json',
      success: function(resource) {
        console.log(JSON.stringify(resource));
      },
      error: function(response) {
        response = JSON.parse(response.responseText);
        console.log('Error: ', response.error.errors[0]);
      }
    });
  }
  jQuery(document).ready(getFeatures);
</script>

Java

In this example, we use HttpURLConnection to communicate with the Maps Engine server. The code loops through the features and prints out the coordinates and Fcility_nam value of each.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;

public class Main {

  public static class FeaturesListResponse {
    public Feature[] features;
  }

  public static class Feature {
    public Geometry geometry;
    public Map properties;
  }

  public static class Geometry {
  }

  public static class PointGeometry extends Geometry {
    public double[] coordinates;
  }

  public static class GeometryDeserializer implements JsonDeserializer {
    public Geometry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
      JsonObject g = json.getAsJsonObject();
      JsonElement type = g.get("type");

      if (type.getAsJsonPrimitive().getAsString().equals("Point")) {
        PointGeometry p = new PointGeometry();
        JsonArray coords = g.getAsJsonArray("coordinates");
        p.coordinates = new double[coords.size()];

        for (int i = 0; i < coords.size(); ++i) {
          p.coordinates[i] = coords.get(i).getAsDouble();
        }
        return p;
      }
      return null;
    }
  }

  public static void main(String[] args) {

    try {

      URL url = new URL("https://www.googleapis.com/mapsengine/v1/tables/12421761926155747447-06672618218968397709/features?maxResults=500&version=published&key=YOUR_API_KEY");
      HttpURLConnection connection = (HttpURLConnection)url.openConnection();
      connection.setRequestMethod("GET");
      connection.setDoOutput(true);
      connection.connect();

      BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

      // Deserialize.
      GsonBuilder builder = new GsonBuilder();
      builder.registerTypeAdapter(Geometry.class, new GeometryDeserializer());
      Gson gson = builder.create();

      FeaturesListResponse map = gson.fromJson(reader, FeaturesListResponse.class);
      PointGeometry g = (PointGeometry)map.features[0].geometry;
      System.out.println(g.coordinates[0] + ", " + g.coordinates[1]);

      System.out.println((String)(map.features[0].properties.get("Fcilty_nam")));
    } catch (IOException e) {
      System.out.println(e.getMessage());
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }
}

Python

In this example, we make a request for all of the features in the specified table, then loop through and display the coordinates for each feature.

import json
import httplib2

http = httplib2.Http()

# Read the location of every Feature in a Table.
url = "https://www.googleapis.com/mapsengine/v1/tables/12421761926155747447-06672618218968397709/features?maxResults=500&version=published&key=YOUR_API_KEY"
resource = None

while resource is None or resource.has("nextPageToken"):
  # Is there an additional page of features to load?
  if resource and resource["nextPageToken"]:
    request = url + '&pageToken=' + resource["nextPageToken"]
  else:
    request = url

  response, content = http.request(request)
  resource = json.loads(content)
  if response.status == 200:
    for feature in resource["features"]:
      print feature["geometry"]["coordinates"]
  else:
    print 'Error: ', resource['error']['errors'][0]['location']

PHP

This PHP example does not require a library. It requests all features from the specified table, then loops through them and displays their coordinates and name.

<?php
  list ($http_code, $response) = GetAsJson("https://www.googleapis.com/mapsengine/v1/tables/12421761926155747447-06672618218968397709/features?maxResults=500&version=published&key=YOUR_API_KEY");
  if ($http_code != 200) {
    print 'Error: ' . $response->error->errors[0]->location;
  } else {
    foreach ($response->features as $feature) {
      print "<b>Location: </b>" . $feature->geometry->coordinates[0] . "<br>";
      print "<b>Name: </b>" . $feature->properties->name . "<br>";
    }
  }

  function GetAsJson($url) {
    $ch = curl_init($url);
    $data = curl_exec($ch);
    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return array($http_status, json_decode($data));
}

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.