forked from GNURub/mapbox-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopen-search.html
132 lines (121 loc) · 3.45 KB
/
open-search.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../core-ajax/core-ajax.html">
<!--
The `open-search` requests are queries composed of location text, zip or lat/lon coordinates that can be used to find corresponding place data..
##### Example:
...
<style>
open-map {
display: block;
height: 100vh;
}
</style>
<open-map latitude="38.867847" longitude="1.310977">
</open-map>
<open-search
query="['1600 pennsylvania ave nw', [-73.989, 40.733], [07800]]">
</open-search>
<script>
var map = document.querySelector('open-map'),
searcher = document.querySelector('open-search');
searcher.accessToken = map.accesToken;
searcher.addEventListener('open-search-results', function(docs){
console.log(docs);
}, false);
</script>
...
@element open-search
@blurb Requests are queries composed of location text, zip or lat/lon coordinates
@status alpha
@homepage https://ruben96.github.io/open-map/components/open-map/
-->
<polymer-element name="open-search" attributes="query result">
<template>
<style>
:host{
display: none;
}
</style>
<core-ajax id="searcher" auto
on-core-response="{{ gotResult }}">
</core-ajax>
</template>
<script>
(function() {
'use-strit';
Polymer({
publish: {
/**
*
* @property accessToken
* @type L.mapbox.accessToken
* @default null
*/
accessToken: {
value: null,
reflect: true
}
},
observe:{
query: 'search',
accessToken: 'search'
},
created: function(){
/**
* The search result.
*
* @attribute result
* @type object
*/
this.result = {};
this.query = [];
},
parseQuery: function(){
var query = '',
subquery;
this.query.forEach(function(element, index){
if(Array.isArray(element)){
subquery = element.join(',');
}else{
subquery = element.toLowerCase()
.split(' ')
.join('+');
}
if(index === 0){
query += subquery;
}else{
query += ';' + subquery;
}
});
return query;
},
search: function() {
if(this.query.length && this.accessToken){
var parsedQuery = this.parseQuery();
this.$.searcher.url = 'http://api.tiles.mapbox.com/v4'+
'/geocode/mapbox.places-v1/'+ parsedQuery +'.json?access_token=' +
this.accessToken;
this.$.searcher.go();
}
},
gotResult: function(e){
var results = [];
JSON.parse(e.detail.response).forEach(function(query){
query.features.forEach(function(place){
results.push(place);
});
});
this.result = {
results: results
};
/**
* Fired when the request was successful.
*
* @event open-search-results
*/
this.fire('open-search-results', this.result);
}
});
})();
</script>
</polymer-element>