From 336e974fe6f4196aaf0b309e805a009ef0fdfd66 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 8 Dec 2016 21:35:54 -0800 Subject: [PATCH] Make map panel pure JS solution --- bower.json | 4 +- panels/map/ha-panel-map.html | 199 +++++++++++++++++++++-------------- 2 files changed, 121 insertions(+), 82 deletions(-) diff --git a/bower.json b/bower.json index 44c126a2bc..d2f7b0a8d0 100644 --- a/bower.json +++ b/bower.json @@ -14,14 +14,14 @@ "font-roboto-local": "~1.0.1", "google-apis": "GoogleWebComponents/google-apis#~1.1.6", "iron-elements": "PolymerElements/iron-elements#~1.0.10", - "leaflet-map": "~1.2.0", "paper-elements": "PolymerElements/paper-elements#~1.0.7", "paper-range-slider": "IftachSadeh/paper-range-slider#~0.2.3", "paper-scroll-header-panel": "~1.0.16", "pikaday": "~1.4.0", "polymer": "Polymer/polymer#~1.7.0", "vaadin-combo-box": "vaadin/vaadin-combo-box#~1.1.5", - "paper-slider": "1.0.11" + "paper-slider": "1.0.11", + "leaflet": "^1.0.2" }, "resolutions": { "paper-slider": "1.0.11" diff --git a/panels/map/ha-panel-map.html b/panels/map/ha-panel-map.html index 055907a8e3..399f7de59d 100644 --- a/panels/map/ha-panel-map.html +++ b/panels/map/ha-panel-map.html @@ -4,9 +4,7 @@ - - - + @@ -14,19 +12,13 @@ - - @@ -111,31 +62,17 @@ Polymer({ type: Array, bindNuclear: function (hass) { return [ - hass.entityGetters.visibleEntityMap, + hass.entityGetters.entityMap, function (entities) { return entities.valueSeq().filter( function (entity) { - return entity.attributes.latitude && entity.state !== 'home'; + return 'latitude' in entity.attributes; } - ).toArray(); - }, - ]; - }, - }, - - zoneEntities: { - type: Array, - bindNuclear: function (hass) { - return [ - hass.entityGetters.entityMap, - function (entities) { - return entities.valueSeq() - .filter(function (entity) { - return entity.domain === 'zone' && !entity.attributes.passive; - }).toArray(); + ); }, ]; }, + observer: 'drawEntities', }, narrow: { @@ -149,16 +86,118 @@ Polymer({ }, attached: function () { - // On Safari, iPhone 5, 5s and some 6 I have observed that the user would be - // unable to pan on initial load. This fixes it. - if (window.L.Browser.mobileWebkit || window.L.Browser.webkit) { - this.async(function () { - var map = this.$.map; - var prev = map.style.display; - map.style.display = 'none'; - this.async(function () { map.style.display = prev; }, 1); - }.bind(this), 1); + var map = this._map = window.L.map(this.$.map); + map.setView([51.505, -0.09], 13); + window.L.tileLayer( + 'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', + { + attribution: '© OpenStreetMap contributors, © CartoDB', + maxZoom: 18, + } + ).addTo(map); + + this.drawEntities(this.locationEntities); + + this.async(function () { + map.invalidateSize(); + this.fitMap(); + }.bind(this), 1); + }, + + fitMap: function () { + var bounds = new window.L.latLngBounds( + this._mapItems.map(function (item) { return item.getLatLng(); })); + + this._map.fitBounds(bounds.pad(0.5)); + }, + + drawEntities: function (entities) { + /* eslint-disable vars-on-top */ + var map = this._map; + if (!map) return; + + if (this._mapItems) { + this._mapItems.forEach(function (marker) { marker.remove(); }); } + var mapItems = this._mapItems = []; + + entities.forEach(function (entity) { + var icon; + + if (entity.domain === 'zone') { + // DRAW ZONE + if (entity.attributes.passive) return; + + // create icon + var iconHTML = ''; + if (entity.attributes.icon) { + iconHTML = ( + ""); + } else { + iconHTML = entity.entityDisplay; + } + + icon = window.L.divIcon({ + html: iconHTML, + iconSize: [24, 24], + className: '', + }); + + // create market with the icon + mapItems.push(window.L.marker( + [entity.attributes.latitude, entity.attributes.longitude], + { + icon, + interactive: false, + title: entity.entityDisplay, + } + ).addTo(map)); + + // create circle around it + mapItems.push(window.L.circle( + [entity.attributes.latitude, entity.attributes.longitude], + { + interactive: false, + color: '#FF9800', + radius: entity.attributes.radius, + } + ).addTo(map)); + + return; + } + + // Filter out entities at home + if (entity.state === 'home' || entity.attributes.hidden) return; + + // DRAW ENTITY + // create icon + icon = window.L.divIcon({ + html: "", + iconSize: [45, 45], + className: '', + }); + + // create market with the icon + mapItems.push(window.L.marker( + [entity.attributes.latitude, entity.attributes.longitude], + { + icon, + title: entity.entityDisplay, + } + ).addTo(map)); + + // create circle around if entity has accuracy + if (entity.attributes.gps_accuracy) { + mapItems.push(window.L.circle( + [entity.attributes.latitude, entity.attributes.longitude], + { + interactive: false, + color: '#0288D1', + radius: entity.attributes.gps_accuracy, + } + ).addTo(map)); + } + }); }, computeMenuButtonClass: function (narrow, showMenu) {