forked from GoogleWebComponents/google-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-map-directions.html
146 lines (129 loc) · 4.12 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!-- Copyright (c) 2014 Google Inc. All rights reserved. -->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../google-apis/google-apis.html">
<!--
Provides the Google Maps API Directions Service to provide directions
between a `startAddress` and `endAddress`.
See https://developers.google.com/maps/documentation/javascript/directions for more
information on the API.
#### Example:
<template is="auto-binding">
<google-map-directions map="{{map}}"
startAddress="San Francisco"
endAddress="Mountain View"
travelMode="TRANSIT"></google-map-directions>
<google-map map="{{map}}" latitude="37.779"
longitude="-122.3892"></google-map>
</template>
@element google-map-directions
@status alpha
@homepage http://googlewebcomponents.github.io/google-map/
-->
<!--
Fired whenever the directions service returns a result.
@event google-map-response
@param {Object} detail
@param {object} detail.response The directions service response.
-->
<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.
*
* @attribute map
* @type google.maps.Map
* @default null
*/
map: null,
/**
* Start address or latlng to get directions from.
*
* @attribute startAddress
* @type string|google.maps.LatLng
* @default null
*/
startAddress: null,
/**
* End address or latlng for directions to end.
*
* @attribute endAddress
* @type string|google.maps.LatLng
* @default null
*/
endAddress: null,
/**
* Travel mode to use. One of 'DRIVING', 'WALKING', 'BICYCLING', 'TRANSIT'.
*
* @attribute travelMode
* @type string
* @default 'DRIVING'
*/
travelMode: 'DRIVING',
/**
* The response from the directions service.
*
* @attribute response
* @type object
*/
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>