-
Notifications
You must be signed in to change notification settings - Fork 2
/
paper-input-location.html
190 lines (175 loc) · 5.67 KB
/
paper-input-location.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../google-apis/google-maps-api.html">
<!--
### paper-input-location
Material design: [Text fields](https://www.google.com/design/spec/components/text-fields.html) with location autocomplete
`<paper-input-location>` is a single-line text field with Material Design styling and location autocomplete.
<paper-input-location label="Place"></paper-input-location>
### Google Maps API
This element work with the Google Maps API. You need a valid API Key. You can get one here: https://developers.google.com/maps/documentation/javascript/get-api-key.
@demo demo/index.html
-->
<dom-module id="paper-input-location">
<template>
<style>
:host {
display: block;
}
</style>
<google-maps-api id="maps" api-key="[[mapsApiKey]]" libraries="places" on-api-load="_onApiLoad"></google-maps-api>
<paper-input
id="inputPaper"
on-value-changed="_resetLocation"
invalid="{{invalid}}"
disabled$="[[disabled]]"
autofocus="[[autofocus]]"
label="[[label]]"
value="{{query}}"
required$="[[required]]"
error-message="[[errorMessage]]">
</paper-input>
</template>
</dom-module>
<script>
Polymer({
is: 'paper-input-location',
/**
* Fired when the user clicks on a place in the Google Map place list.
*
* @event place-clicked
*/
properties: {
/**
* Actual value of the paper-input
*/
query: {
type: String,
notify: true
},
/**
* Option for the Autocomplete Google Maps API
*/
autocompleteOptions: {
type: Object,
value: function() {
return {};
}
},
/**
* Place object is set after the user click on one Google Maps autocomplete result.
*/
place: {
type: Object,
value: function() {
return {};
},
notify: true
},
/**
* Computed object with latitude and longitude
*/
locationGeoPos: {
type: Object,
computed: '_computeLocation(longitude, latitude)'
},
/**
* Location formatted address of the last clicked place.
*/
locationName: {
type: String,
notify: true
},
/**
* Latitude of the last clicked place.
*/
latitude: {
type: Number,
notify: true
},
/**
* Longitude of the last clicked place.
*/
longitude: {
type: Number,
notify: true
},
/**
* Set to true to disable this input.
*/
disabled: {
type: Boolean,
default: false
},
/**
* Set to true to mark the input as required.
*/
required: {
type: Boolean,
default: false
},
/**
* Returns true if the value is invalid
*/
invalid: {
type: Boolean,
value: false,
notify: true
},
/**
* Google Maps api key. To obtain an API key, see https://developers.google.com/maps/documentation/javascript/get-api-key.
*/
mapsApiKey: String,
/**
* The label for this input.
*/
label: String,
/**
* Autofocus propertie
*/
autofocus: Boolean,
/**
* The error message to display when the input is invalid.
*/
errorMessage: String,
},
/**
* Validate if the input is filled with a valid Google Map address.
*/
validatePlace: function() {
if (this.place.geometry) {
return true;
} else {
this.$.inputPaper.invalid = true;
return false;
}
},
_computeLocation: function(longitude, latitude) {
return {
'lng': longitude,
'lat': latitude
}
},
_resetLocation: function() {
this.place = {};
},
_onApiLoad: function() {
var rawInput;
if (this.$.inputPaper.querySelector('#input')) {
rawInput = this.$.inputPaper.querySelector('#input');
} else {
rawInput = this.$.inputPaper.root.querySelector('#input');
}
this.autocomplete = new google.maps.places.Autocomplete(rawInput, this.options);
google.maps.event.addListener(this.autocomplete, 'place_changed', this._onChangePlace.bind(this));
},
_onChangePlace: function() {
this.$.inputPaper.invalid = false;
this.place = this.autocomplete.getPlace();
this.locationName = this.autocomplete.getPlace().formatted_address;
this.latitude = this.autocomplete.getPlace().geometry.location.lat();
this.longitude = this.autocomplete.getPlace().geometry.location.lng();
this.fire('place-clicked');
}
});
</script>