| OLD | NEW |
| (Empty) | |
| 1 <!-- Copyright (c) 2015 Google Inc. All rights reserved. --> |
| 2 |
| 3 <link rel="import" href="../polymer/polymer.html"> |
| 4 <link rel="import" href="../google-apis/google-maps-api.html"> |
| 5 |
| 6 <!-- |
| 7 The `google-map-marker` element represents a map marker. It is used as a |
| 8 child of `google-map`. |
| 9 |
| 10 <b>Example</b>: |
| 11 |
| 12 <google-map latitude="37.77493" longitude="-122.41942"> |
| 13 <google-map-marker latitude="37.779" longitude="-122.3892" |
| 14 title="Go Giants!"></google-map-marker> |
| 15 </google-map> |
| 16 |
| 17 <b>Example</b> - marker with info window (children create the window content): |
| 18 |
| 19 <google-map-marker latitude="37.77493" longitude="-122.41942"> |
| 20 <img src="image.png"> |
| 21 </google-map-marker> |
| 22 |
| 23 <b>Example</b> - a draggable marker: |
| 24 |
| 25 <google-map-marker latitude="37.77493" longitude="-122.41942" |
| 26 draggable="true"></google-map-marker> |
| 27 |
| 28 <b>Example</b> - hide a marker: |
| 29 |
| 30 <google-map-marker latitude="37.77493" longitude="-122.41942" |
| 31 hidden></google-map-marker> |
| 32 |
| 33 --> |
| 34 |
| 35 <dom-module id="google-map-marker"> |
| 36 <style> |
| 37 :host { |
| 38 display: none; |
| 39 } |
| 40 </style> |
| 41 <template><content></content></template> |
| 42 </dom-module> |
| 43 |
| 44 <script> |
| 45 |
| 46 (function() { |
| 47 Polymer({ |
| 48 |
| 49 is: 'google-map-marker', |
| 50 |
| 51 /** |
| 52 * Fired when the marker icon was clicked. Requires the clickEvents attribut
e to be true. |
| 53 * @param {google.maps.MouseEvent} event The mouse event. |
| 54 * @event google-map-marker-click |
| 55 */ |
| 56 /** |
| 57 * Fired when the marker icon was double clicked. Requires the clickEvents a
ttribute to be true. |
| 58 * @param {google.maps.MouseEvent} event The mouse event. |
| 59 * @event google-map-marker-dblclick |
| 60 */ |
| 61 /** |
| 62 * Fired for a mousedown on the marker. Requires the mouseEvents attribute t
o be true. |
| 63 * @event google-map-marker-mousedown |
| 64 * @param {google.maps.MouseEvent} event The mouse event. |
| 65 */ |
| 66 /** |
| 67 * Fired when the DOM `mousemove` event is fired on the marker. Requires the
mouseEvents |
| 68 * attribute to be true. |
| 69 * @event google-map-marker-mousemove |
| 70 * @param {google.maps.MouseEvent} event The mouse event. |
| 71 */ |
| 72 /** |
| 73 * Fired when the mouse leaves the area of the marker icon. Requires the mou
seEvents attribute to be |
| 74 * true. |
| 75 * @event google-map-marker-mouseout |
| 76 * @param {google.maps.MouseEvent} event The mouse event. |
| 77 */ |
| 78 /** |
| 79 * Fired when the mouse enters the area of the marker icon. Requires the mou
seEvents attribute to be |
| 80 * true. |
| 81 * @event google-map-marker-mouseover |
| 82 * @param {google.maps.MouseEvent} event The mouse event. |
| 83 */ |
| 84 /** |
| 85 * Fired for a mouseup on the marker. Requires the mouseEvents attribute to
be true. |
| 86 * |
| 87 * @event google-map-marker-mouseup |
| 88 * @param {google.maps.MouseEvent} event The mouse event. |
| 89 */ |
| 90 /** |
| 91 * Fired for a rightclick on the marker. Requires the clickEvents attribute
to be true. |
| 92 * @event google-map-marker-rightclick |
| 93 * @param {google.maps.MouseEvent} event The mouse event. |
| 94 */ |
| 95 properties: { |
| 96 /** |
| 97 * A Google Maps marker object. |
| 98 * @type google.maps.Marker |
| 99 */ |
| 100 marker: Object, |
| 101 |
| 102 /** |
| 103 * The Google map object. |
| 104 * @type google.maps.Map |
| 105 */ |
| 106 map: { |
| 107 type: Object, |
| 108 observer: '_mapChanged' |
| 109 }, |
| 110 |
| 111 /** |
| 112 * A Google Map Infowindow object. |
| 113 */ |
| 114 info: { |
| 115 type: Object, |
| 116 value: null |
| 117 }, |
| 118 |
| 119 /** |
| 120 * When true, marker *click events are automatically registered. |
| 121 */ |
| 122 clickEvents: { |
| 123 type: Boolean, |
| 124 value: false, |
| 125 observer: '_clickEventsChanged' |
| 126 }, |
| 127 |
| 128 /** |
| 129 * Image URL for the marker icon. |
| 130 * @type string|google.maps.Icon|google.maps.Symbol |
| 131 */ |
| 132 icon: { |
| 133 type: Object, |
| 134 value: null, |
| 135 observer: '_iconChanged' |
| 136 }, |
| 137 |
| 138 /** |
| 139 * When true, marker mouse* events are automatically registered. |
| 140 */ |
| 141 mouseEvents: { |
| 142 type: Boolean, |
| 143 value: false, |
| 144 observer: '_mouseEventsChanged' |
| 145 }, |
| 146 |
| 147 /** |
| 148 * Z-index for the marker icon. |
| 149 */ |
| 150 zIndex: { |
| 151 type: Number, |
| 152 value: 0, |
| 153 observer: '_zIndexChanged' |
| 154 }, |
| 155 |
| 156 /** |
| 157 * The marker's longitude coordinate. |
| 158 */ |
| 159 longitude: { |
| 160 type: Number, |
| 161 value: null, |
| 162 reflectToAttribute: true |
| 163 }, |
| 164 /** |
| 165 * The marker's latitude coordinate. |
| 166 */ |
| 167 latitude: { |
| 168 type: Number, |
| 169 value: null, |
| 170 reflectToAttribute: true |
| 171 } |
| 172 }, |
| 173 |
| 174 observers: [ |
| 175 '_updatePosition(latitude, longitude)' |
| 176 ], |
| 177 |
| 178 detached: function() { |
| 179 if (this.marker) { |
| 180 this.marker.setMap(null); |
| 181 } |
| 182 if (this._contentObserver) |
| 183 this._contentObserver.disconnect(); |
| 184 }, |
| 185 |
| 186 attached: function() { |
| 187 // If element is added back to DOM, put it back on the map. |
| 188 if (this.marker) { |
| 189 this.marker.setMap(this.map); |
| 190 } |
| 191 }, |
| 192 |
| 193 _updatePosition: function() { |
| 194 if (this.marker && this.latitude != null && this.longitude != null) { |
| 195 this.marker.setPosition({ |
| 196 lat: parseFloat(this.latitude), |
| 197 lng: parseFloat(this.longitude) |
| 198 }); |
| 199 } |
| 200 }, |
| 201 |
| 202 _clickEventsChanged: function() { |
| 203 if (this.map) { |
| 204 if (this.clickEvents) { |
| 205 this._forwardEvent('click'); |
| 206 this._forwardEvent('dblclick'); |
| 207 this._forwardEvent('rightclick'); |
| 208 } else { |
| 209 this._clearListener('click'); |
| 210 this._clearListener('dblclick'); |
| 211 this._clearListener('rightclick'); |
| 212 } |
| 213 } |
| 214 }, |
| 215 |
| 216 _mouseEventsChanged: function() { |
| 217 if (this.map) { |
| 218 if (this.mouseEvents) { |
| 219 this._forwardEvent('mousedown'); |
| 220 this._forwardEvent('mousemove'); |
| 221 this._forwardEvent('mouseout'); |
| 222 this._forwardEvent('mouseover'); |
| 223 this._forwardEvent('mouseup'); |
| 224 } else { |
| 225 this._clearListener('mousedown'); |
| 226 this._clearListener('mousemove'); |
| 227 this._clearListener('mouseout'); |
| 228 this._clearListener('mouseover'); |
| 229 this._clearListener('mouseup'); |
| 230 } |
| 231 } |
| 232 }, |
| 233 |
| 234 _iconChanged: function() { |
| 235 if (this.marker) { |
| 236 this.marker.setIcon(this.icon); |
| 237 } |
| 238 }, |
| 239 |
| 240 _zIndexChanged: function() { |
| 241 if (this.marker) { |
| 242 this.marker.setZIndex(this.zIndex); |
| 243 } |
| 244 }, |
| 245 |
| 246 _mapChanged: function() { |
| 247 // Marker will be rebuilt, so disconnect existing one from old map and lis
teners. |
| 248 if (this.marker) { |
| 249 this.marker.setMap(null); |
| 250 google.maps.event.clearInstanceListeners(this.marker); |
| 251 } |
| 252 |
| 253 if (this.map && this.map instanceof google.maps.Map) { |
| 254 this._mapReady(); |
| 255 } |
| 256 }, |
| 257 |
| 258 _contentChanged: function() { |
| 259 if (this._contentObserver) |
| 260 this._contentObserver.disconnect(); |
| 261 // Watch for future updates. |
| 262 this._contentObserver = new MutationObserver( this._contentChanged.bind(th
is)); |
| 263 this._contentObserver.observe( this, { |
| 264 childList: true, |
| 265 subtree: true |
| 266 }); |
| 267 |
| 268 var content = this.innerHTML.trim(); |
| 269 if (content) { |
| 270 if (!this.info) { |
| 271 // Create a new infowindow |
| 272 this.info = new google.maps.InfoWindow(); |
| 273 this.infoHandler_ = google.maps.event.addListener(this.marker, 'click'
, function() { |
| 274 this.info.open(this.map, this.marker); |
| 275 }.bind(this)); |
| 276 } |
| 277 this.info.setContent(content); |
| 278 } else { |
| 279 if (this.info) { |
| 280 // Destroy the existing infowindow. It doesn't make sense to have an
empty one. |
| 281 google.maps.event.removeListener(this.infoHandler_); |
| 282 this.info = null; |
| 283 } |
| 284 } |
| 285 }, |
| 286 |
| 287 _mapReady: function() { |
| 288 this._listeners = {}; |
| 289 this.marker = new google.maps.Marker({ |
| 290 map: this.map, |
| 291 position: new google.maps.LatLng(this.latitude, this.longitude), |
| 292 title: this.title, |
| 293 draggable: this.draggable, |
| 294 visible: !this.hidden, |
| 295 icon: this.icon, |
| 296 zIndex: this.zIndex |
| 297 }); |
| 298 this._contentChanged(); |
| 299 this._clickEventsChanged(); |
| 300 this._contentChanged(); |
| 301 this._mouseEventsChanged(); |
| 302 setupDragHandler_.bind(this)(); |
| 303 }, |
| 304 |
| 305 _clearListener: function(name) { |
| 306 if (this._listeners[name]) { |
| 307 google.maps.event.removeListener(this._listeners[name]); |
| 308 this._listeners[name] = null; |
| 309 } |
| 310 }, |
| 311 |
| 312 _forwardEvent: function(name) { |
| 313 this._listeners[name] = google.maps.event.addListener(this.marker, name, f
unction(event) { |
| 314 this.fire('google-map-marker-' + name, event); |
| 315 }.bind(this)); |
| 316 }, |
| 317 |
| 318 attributeChanged: function(attrName, oldVal, newVal) { |
| 319 if (!this.marker) { |
| 320 return; |
| 321 } |
| 322 |
| 323 // Cannot use *Changed watchers for native properties. |
| 324 switch (attrName) { |
| 325 case 'hidden': |
| 326 this.marker.setVisible(!this.hidden); |
| 327 break; |
| 328 case 'draggable': |
| 329 this.marker.setDraggable(this.draggable); |
| 330 setupDragHandler_.bind(this)(); |
| 331 break; |
| 332 case 'title': |
| 333 this.marker.setTitle(this.title); |
| 334 break; |
| 335 } |
| 336 } |
| 337 }); |
| 338 |
| 339 function setupDragHandler_() { |
| 340 if (this.draggable) { |
| 341 this.dragHandler_ = google.maps.event.addListener( |
| 342 this.marker, 'dragend', onDragEnd_.bind(this)); |
| 343 } else { |
| 344 google.maps.event.removeListener(this.dragHandler_); |
| 345 this.dragHandler_ = null; |
| 346 } |
| 347 } |
| 348 |
| 349 function onDragEnd_(e, details, sender) { |
| 350 this.latitude = e.latLng.lat(); |
| 351 this.longitude = e.latLng.lng(); |
| 352 } |
| 353 })(); |
| 354 </script> |
| OLD | NEW |