Blame view

bower_components/google-map/google-map-search.html 5.63 KB
73bcce88   luigser   COMPONENTS
1
2
3
4
5
6
7
8
9
10
11
12
13
  <!-- Copyright (c) 2015 Google Inc. All rights reserved. -->
  
  <link rel="import" href="../polymer/polymer.html">
  
  <!--
  `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">
eb240478   Luigi Serra   public room cards...
14
15
        <google-map-search map="[[map]]" query="Pizza" results="{{results}}">
        </google-map-search>
73bcce88   luigser   COMPONENTS
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
        <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>
   -->
  
  <script>
  
    Polymer({
  
      is: 'google-map-search',
  
      properties: {
  
        /**
         * The Google map object.
         */
        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: function() { return []; },
          notify: true
        },
  
        /**
         * The lat/lng location.
         */
        location: {
          type: Object,
          value: null,
e619a3b0   Luigi Serra   Controllet cross ...
122
          readOnly: true
73bcce88   luigser   COMPONENTS
123
124
125
126
127
128
129
130
131
        }
      },
  
      observers: [
        'search(query,map,location,radius,types,globalSearch)',
        '_updateLocation(latitude,longitude)'
      ],
  
      /**
e619a3b0   Luigi Serra   Controllet cross ...
132
133
134
       * Fired when the details of a place are returned.
       *
       * @event google-map-search-place-detail
eb240478   Luigi Serra   public room cards...
135
       * @param {google.maps.MarkerPlace} detail The place details.
e619a3b0   Luigi Serra   Controllet cross ...
136
137
138
       */
  
      /**
73bcce88   luigser   COMPONENTS
139
140
141
       * Fired when the search element returns a result.
       *
       * @event google-map-search-results
eb240478   Luigi Serra   public room cards...
142
       * @param {Array<{latitude: number, longitude: number}>} detail An array of search results
73bcce88   luigser   COMPONENTS
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
       */
  
      /**
       * Perform a search using for `query` for the search term.
       */
      search: function() {
        if (this.query && this.map) {
          var places = new google.maps.places.PlacesService(this.map);
  
          if (this.types && typeof this.types == 'string') {
            var types = this.types.split(' ');
          }
  
          if (!this.globalSearch) {
            var bounds = this.map.getBounds();
          } else if (this.radius) {
            var radius = this.radius;
            var location = this.location ? this.location : this.map.getCenter();
          }
  
          places.textSearch({
            query: this.query,
            types: types,
            bounds: bounds,
            radius: radius,
            location: location
          }, this._gotResults.bind(this));
        }
      },
  
e619a3b0   Luigi Serra   Controllet cross ...
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
      /**
       * Fetches details for a given place.
       * @param {String} placeId The place id.
       * @return {Promise} place The place information.
       */
      getDetails: function(placeId) {
        var places = new google.maps.places.PlacesService(this.map);
  
        return new Promise(function(resolve, reject) {
          places.getDetails({placeId: placeId}, function(place, status) {
            if (status === google.maps.places.PlacesServiceStatus.OK) {
              resolve(place);
              this.fire('google-map-search-place-detail', place);
            } else {
              reject(status);
            }
          }.bind(this));
        }.bind(this));
      },
  
73bcce88   luigser   COMPONENTS
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
      _gotResults: function(results, status) {
        this.results = results.map(function(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: function() {
        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});
      }
    });
  </script>