Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur quis dolor vel arcu blandit tristique. Proin vestibulum nec felis non fringilla. Pellentesque vulputate dui ut risus bibendum, sed egestas arcu ullamcorper. Quisque eget eros pellentesque, aliquet tortor placerat, vehicula lectus. Fusce sit amet mattis turpis, et tempus orci. Vestibulum mauris velit, vulputate a risus quis, imperdiet hendrerit ante. Nunc sollicitudin risus tortor, ac venenatis sem volutpat malesuada. Mauris neque metus, ornare eget porta id, tincidunt vitae magna. In scelerisque quam auctor maximus pellentesque. Sed laoreet ex mi, vel lacinia urna consectetur id. Sed est quam, finibus eget orci in, vulputate tempus diam
In this codelab, You'll create a fully working Google Maps app using elements in Polymer's Google Web Components collection. The app will be responsive, include driving directions, and transit a mode. Along the way, you'll also learn about Polymer's data-binding features and iron element set.
The first time you run Chrome Dev Editor it will ask you to setup your workspace environment.
Fire up Chrome Dev Editor and start a new project:
Chrome Dev Editor creates a basic scaffold for your Polymer app. In the background, it also uses Bower to download and install a list of dependencies (including the Polymer core library) into the bower_components
/ folder. Fetching the components make take some time if your internet connection is slow. You'll learn more about using Bower in the next step.
PolymerMapsCodelab/
bower_components/ <!-- installed dependencies from Bower -->
bower.json <!-- Bower metadata files used for managing deps -->
index.html <!-- your app -->
main.js
styles.css
At any point, select the index.html file and hit the button in the top toolbar to run the app. Chrome Dev Editor fires up a web server and navigates to the index.html page. This is great way to preview changes as you make them.
At this point the app doesn't do much. Let's add a map!
The Google Web Components provide the <google-map>
element for declaratively rendering a Google Map. To use it, you first need to install it using Bower.
Normally, you'd run bower install GoogleWebComponents/google-map --save
on the command line to install <google-map>
and save it as a dependency. However, Chrome Dev Editor does not have a command line for running Bower commands. Instead, you need to manually edit bower.json
to include google-map
, then run Chrome Dev Editor's Bower Update feature. Bower Update checks the dependencies in bower.json
and installs any missing ones.
bower.json
and add google-map
to the dependencies
object:"dependencies": {
"iron-elements": "PolymerElements/iron-elements#^1.0.0",
"paper-elements": "PolymerElements/paper-elements#^1.0.1",
"google-map": "GoogleWebComponents/google-map#^1.0.3"
}
bower.json
filename in the editor.The download may take few seconds. You can verify that <google-map>
(and any dependencies) were installed by checking that bower_components/google-map/
was created and populated.
To employ <google-map>
, you need to:
index.html
.In index.html
, remove all other HTML imports in the <head> and replace them with a single import that loads google-map.html
:
<head>
....
<script src="bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="bower_components/google-map/google-map.html">
</head>
Next, replace the contents of <body>
with an instance of <google-map>
:
<body unresolved>
<google-map latitude="37.779" longitude="-122.3892" zoom="13"></google-map>
</body>
As you can see, using <google-map>
is completely declarative! The map is centered using the latitude
and longitude
attributes and its zoom level is set by the zoom
attribute.
If you run the app right now, nothing will display. In order for the map to properly display itself, you need to set its container (in this case, <body>
) to have a fixed height.
Open styles.css
and replace its contents with default styling:
body, html {
font-family: 'Roboto', Arial, sans-serif;
height: 100%;
margin: 0;
}
<google-map>
supports adding map markers to the map by declaring <google-map-marker>
elements as children. The marker locations are also set using latitude
and longitude
attributes.
Back in index.html, add a draggable <google-map-marker>
to the map:
<google-map latitude="37.779" longitude="-122.3892" zoom="13" disable-default-ui>
<google-map-marker latitude="37.779" longitude="-122.3892"
title="Go Giants!" draggable="true"></google-map-marker>
</google-map>
Notice that we've also disabled the map's controls by setting disableDefaultUi
to true. Since it's a boolean property, its presence as an HTML attribute makes it truthy.
If you haven't already done so, hit the button. At this point, you should see a map that takes up the entire viewport and has a single marker pin.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur quis dolor vel arcu blandit tristique. Proin vestibulum nec felis non fringilla. Pellentesque vulputate dui ut risus bibendum, sed egestas arcu ullamcorper. Quisque eget eros pellentesque, aliquet tortor placerat, vehicula lectus. Fusce sit amet mattis turpis, et tempus orci. Vestibulum mauris velit, vulputate a risus quis, imperdiet hendrerit ante. Nunc sollicitudin risus tortor, ac venenatis sem volutpat malesuada. Mauris neque metus, ornare eget porta id, tincidunt vitae magna. In scelerisque quam auctor maximus pellentesque. Sed laoreet ex mi, vel lacinia urna consectetur id. Sed est quam, finibus eget orci in, vulputate tempus diam