第 1 張,共 32 張

My First Google Maps API

Earth Engine User Summit 2017�12-14 June 2017

Christiaan Adams�Google Earth & Google Earth Outreach Team

第 2 張,共 32 張

What you need to get started

  • Text editor

  • Browser (all are fine, but Chrome and Firefox have great debugging features)

  • Time to practice!

  • Patience: there will be a lot of trial-and-error.

  • "Friends" who program - bake them cookies!

第 3 張,共 32 張

第 4 張,共 32 張

Note: for the future, you’ll need to get an API key for your own website!

Scroll down for “Hello, World” example & walk-through.

第 5 張,共 32 張

第 6 張,共 32 張

<!DOCTYPE html>�<html>� <head>� <title>Simple Map</title>� <meta name="viewport" content="initial-scale=1.0">� <meta charset="utf-8">� <style>� #map {� height: 100%;� }

html, body {� height: 100%;� margin: 0;� padding: 0;� }� </style>� </head>� <body>� <div id="map"></div>� <script>� var map;� function initMap() {� map = new google.maps.Map(document.getElementById('map'), {� center: {lat: -34.397, lng: 150.644},� zoom: 8� });� }� </script>� <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"� async defer></script>� </body>�</html>

第 7 張,共 32 張

  1. Copy the sample code (HTML and Javascript) from the website.�
  2. Paste it into a text editor (e.g. Notepad++ or TextMate).�
  3. Save to a folder, as firstapi.html or index.html
  4. Go to that folder, and open the HTML page you just created!

第 8 張,共 32 張

Oops…

See the developer tools - javascript console �(F12 or Ctrl-Shift-I)

Invalid API Key!

For now, just remove that part of the script URL, to make the first line below look like the second one:

第 9 張,共 32 張

Hooray, a map!

第 10 張,共 32 張

1. We declare the application as HTML5 using the <!DOCTYPTE html> declaration.

6. We include the Maps API JavaScript code

file (js) using another <script> tag.

5. We create a JavaScript “map” object, passing it

the <div> element and a javascript object

containing a number of map properties.

3. We create a <div> element named “map” to hold the Map.

2. We set up some CSS styles to position elements on the page.

4. In a <script> tag, we create a function which initializes the map.

7. The js file loads asynchronously, and calls our initMap() function when ready.

第 11 張,共 32 張

This is where your API Key will go for production.

Starting zoom level.

Styles:

The map div is set to fill the whole page vertically.

Starting centerpoint location for the map.

第 12 張,共 32 張

Change the map styles to read:

#map {

height: 600px;

width: 800px;

}

Change the centerpoint & zoom:

center: {lat: 37.4036, lng: -122.0325},

zoom: 17

Let’s make some changes!

Add a title to your page, just above the map div:

<h1>My First Maps API Page</h1>

<div id="map"></div>

第 13 張,共 32 張

第 14 張,共 32 張

第 15 張,共 32 張

Add the mapTypeId:

mapTypeId: 'satellite'

Tip: Don’t forget the comma after each item in the list, except the last one!

Save your file and reload your page to see the changes.

第 16 張,共 32 張

第 17 張,共 32 張

第 18 張,共 32 張

Default Control Set

  • Default Control Set: automatically adds controls to the map.
  • It sizes them based on the map size.

  • If you want to remove controls, you have to specifically remove them in the code.
  • Some controls only appear if you specifically add them in the code.
  • Some controls have options to specify the size or type.

第 19 張,共 32 張

Try adding:

zoomControl: true,

mapTypeControl: true,

mapTypeControlOptions: {

style: google.maps.MapTypeControlStyle.DROPDOWN_MENU

},

scaleControl: true

第 20 張,共 32 張

Overlay Types:

  • Markers (points)
  • Polylines
  • Polygons (including circles & rectangles)
  • Info windows
  • Ground overlay & custom overlay (lay image on the map)
  • Overlay map types & Custom map types (tiles)

第 21 張,共 32 張

第 22 張,共 32 張

Add a marker!

Put the code inside the initMap function, and after the map declaration & options.

var myLatLng = {lat: 37.4044, lng: -122.0314};

var marker = new google.maps.Marker({

position: myLatLng,

map: map,

title: 'Hello World!'

});

第 23 張,共 32 張

Try adding a different icon!

var image = 'http://maps.google.com/mapfiles/kml/shapes/sunny.png';

var marker = new google.maps.Marker({

position: myLatLng,

map: map,

icon: image,

title: 'Hello World!'

});

第 24 張,共 32 張

第 25 張,共 32 張

Size and complexity restrictions for KML rendering:

  • Maximum fetched file size: 3 MB
  • Maximum uncompressed KML file size (extracted from KMZ): 10 MB
  • Maximum number of total document-wide features: 1,000
  • Maximum number of network Links: 10
  • Number of KML layers depends on URL length, usually about 10-20
  • Supported KML elements - not all KML elements are supported in Maps

第 26 張,共 32 張

In Earth Engine, we can�export vectors to KML�in a GCS Bucket

第 27 張,共 32 張

Add a KML layer:

var LanduseSamples = new google.maps.KmlLayer({

url:'https://storage.googleapis.com/ee-demos/eeus/LanduseFeaturesee_export.kml',

map: map

});

第 28 張,共 32 張

Export raster�tiles to GCS�(eg: NDVI)

第 29 張,共 32 張

Tiles in GCS

第 30 張,共 32 張

Add a Raster Tile layer (overlayMapType method):

var minZoom = 0;

var maxZoom = 14;

var tilePrefix = 'https:\/\/storage.googleapis.com\/ee-demos\/eeus\/tiles\/NDVI2012\/';

var tileSuffix = '';

var overlayMapType = new google.maps.ImageMapType({

getTileUrl: function(coord, zoom) {

if (zoom < minZoom || zoom > maxZoom) {

return null;

}

var numTiles = 1 << zoom;

var x = ((coord.x % numTiles) + numTiles) % numTiles;

return [tilePrefix, zoom, '/', x, '/', coord.y, tileSuffix].join('');

},

tileSize: new google.maps.Size(256, 256),

});

map.overlayMapTypes.push(overlayMapType);

第 31 張,共 32 張

第 32 張,共 32 張

Thank you