forked from JohnRiv/google-map
-
Notifications
You must be signed in to change notification settings - Fork 2
/
google-map-search.js
209 lines (187 loc) · 5.23 KB
/
google-map-search.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { Polymer } from '@polymer/polymer/lib/legacy/polymer-fn.js';
/* Copyright (c) 2015 Google Inc. All rights reserved. */
/*
`google-map-search` provides Google Maps Places API functionality.
See https://developers.google.com/maps/documentation/javascript/places for more
information on the API.
#### Example:
<template is="dom-bind">
<google-map-search map="[[map]]" query="Pizza" results="{{results}}">
</google-map-search>
<google-map map="{{map}}" latitude="37.779"
longitude="-122.3892">
<template is="dom-repeat" items="{{results}}" as="marker">
<google-map-marker latitude="{{marker.latitude}}"
longitude="{{marker.longitude}}">
<h2>{{marker.name}}</h2>
<span>{{marker.formatted_address}}</span>
</google-map-marker>
</template>
</google-map>
</template>
*/
Polymer({
is: 'google-map-search',
properties: {
/**
* The Google map object.
*
* @type google.maps.Map
*/
map: {
type: Object,
value: null,
},
/**
* The search query.
*/
query: {
type: String,
value: null,
},
/**
* Latitude of the center of the search area.
* Ignored if `globalSearch` is true.
*/
latitude: {
type: Number,
value: null,
},
/**
* Longitude of the center of the search area.
* Ignored if `globalSearch` is true.
*/
longitude: {
type: Number,
value: null,
},
/**
* Search radius, in meters.
* If `latitude` and `longitude` are not specified,
* the center of the currently visible map area is used.
*
* If not set, search will be restricted to the currently visible
* map area, unless `globalSearch` is set to true.
*/
radius: {
type: Number,
value: null,
},
/**
* By default, search is restricted to the currently visible map area.
* Set this to true to search everywhere.
*
* Ignored if `radius` is set.
*/
globalSearch: {
type: Boolean,
value: false,
},
/**
* Space-separated list of result types.
* The search will only return results of the listed types.
* See https://developers.google.com/places/documentation/supported_types
* for a list of supported types.
* Leave empty or null to search for all result types.
*/
types: {
type: String,
value: null,
},
/**
* The search results.
*/
results: {
type: Array,
value() { return []; },
notify: true,
},
/**
* The lat/lng location.
*/
location: {
type: Object,
value: null,
readOnly: true,
},
},
observers: [
'search(query,map,location,radius,types,globalSearch)',
'_updateLocation(latitude,longitude)',
],
/**
* Fired when the details of a place are returned.
*
* @event google-map-search-place-detail
* @param {google.maps.MarkerPlace} detail The place details.
*/
/**
* Fired when the search element returns a result.
*
* @event google-map-search-results
* @param {Array<{latitude: number, longitude: number}>} detail An array of search results
*/
/**
* Perform a search using for `query` for the search term.
*/
search() {
if (this.query && this.map) {
const places = new google.maps.places.PlacesService(this.map);
if (this.types && typeof this.types === 'string') {
var types = this.types.split(' ');
}
if (this.radius) {
var radius = this.radius;
var location = this.location ? this.location : this.map.getCenter();
} else if (!this.globalSearch) {
var bounds = this.map.getBounds();
}
places.textSearch({
query: this.query,
types,
bounds,
radius,
location,
}, this._gotResults.bind(this));
}
},
/**
* Fetches details for a given place.
*
* @param {String} placeId The place id.
* @return {Promise} place The place information.
*/
getDetails(placeId) {
const places = new google.maps.places.PlacesService(this.map);
return new Promise(((resolve, reject) => {
places.getDetails({ placeId }, (place, status) => {
if (status === google.maps.places.PlacesServiceStatus.OK) {
resolve(place);
this.fire('google-map-search-place-detail', place);
} else {
reject(status);
}
});
}));
},
_gotResults(results, status) {
this.results = results.map((result) => {
// obtain lat/long from geometry
result.latitude = result.geometry.location.lat();
result.longitude = result.geometry.location.lng();
return result;
});
this.fire('google-map-search-results', this.results);
},
_updateLocation() {
if (!this.map) {
return;
} else if (typeof this.latitude !== 'number' || isNaN(this.latitude)) {
throw new TypeError('latitude must be a number');
} else if (typeof this.longitude !== 'number' || isNaN(this.longitude)) {
throw new TypeError('longitude must be a number');
}
// Update location. This will trigger a new search.
this._setLocation({ lat: this.latitude, lng: this.longitude });
},
});