forked from GoogleWebComponents/google-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-map-directions.html
113 lines (104 loc) · 3.54 KB
/
google-map-directions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE
The complete set of authors may be found at http://polymer.github.io/AUTHORS
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../google-apis/google-apis.html">
<polymer-element name="google-map-directions" attributes="map startAddress endAddress travelMode response">
<template>
<style>
:host {
display: none;
}
</style>
<google-maps-api on-api-load="{{mapApiLoaded}}"></google-maps-api>
</template>
<script>
Polymer('google-map-directions', {
/**
* The Google map object.
*
* @property map
* @type google.maps.Map
* @default null
*/
map: null,
/**
* Start address or latlng to get directions from.
*
* @property startAddress
* @type string|google.maps.LatLng
* @default null
*/
startAddress: null,
/**
* End address or latlng for directions to end.
*
* @property endAddress
* @type string|google.maps.LatLng
* @default null
*/
endAddress: null,
/**
* Travel mode to use. One of 'DRIVING', 'WALKING', 'BICYCLING', 'TRANSIT'.
*
* @attribute travelMode
* @type string
* @detault DRIVING
*/
travelMode: 'DRIVING',
observe: {
startAddress: 'route',
endAddress: 'route',
travelMode: 'route'
},
mapApiLoaded: function() {
this.route();
},
responseChanged: function() {
if (this.directionsRenderer && this.response) {
this.directionsRenderer.setDirections(this.response);
}
},
mapChanged: function() {
if (this.map && this.map instanceof google.maps.Map) {
if (!this.directionsRenderer) {
this.directionsRenderer = new google.maps.DirectionsRenderer();
}
this.directionsRenderer.setMap(this.map);
this.responseChanged();
} else {
// If there is no more map, remove the directionsRenderer from the map and delete it.
this.directionsRenderer.setMap(null);
this.directionsRenderer = null;
}
},
route: function() {
// Abort attempts to route if the API is not available yet or the required attributes are blank.
if (typeof google == 'undefined' || typeof google.maps == 'undefined' || !this.startAddress || !this.endAddress) {
return;
}
// Construct a directionsService if necessary.
// Wait until here where the maps api has loaded and directions are actually needed.
if (!this.directionsService) {
this.directionsService = new google.maps.DirectionsService();
}
var request = {
origin: this.startAddress,
destination: this.endAddress,
travelMode: this.travelMode
};
this.directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
this.response = response;
this.fire('google-map-response', {response: response});
}
}.bind(this));
}
});
</script>
</polymer-element>