From e6dda463ee77fdf1267146439367095b5f720aa7 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Tue, 25 Feb 2020 10:31:38 +0000 Subject: [PATCH] Surprising, works! --- resources/public/index.html | 10 +++--- resources/public/js/geocsv.js | 59 ++++++++++++++++++++++------------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/resources/public/index.html b/resources/public/index.html index a7413ef..fc55cb9 100644 --- a/resources/public/index.html +++ b/resources/public/index.html @@ -1,7 +1,7 @@ - GeoCSV Lite + GeoCSV JS @@ -11,9 +11,9 @@ integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIpt crossorigin=""/> -

GeoCSV Lite

+

GeoCSV JS

- An ultra-lightweight tool to show comma-separated value data on a map + An even more ultra-lightweight tool to show comma-separated value data on a map

@@ -184,8 +184,6 @@ crossorigin=""> /* Map using data from element content */ GeoCSV.initialise_map_element("element-content-map", document.getElementById("element-content-map").innerText); - - + diff --git a/resources/public/js/geocsv.js b/resources/public/js/geocsv.js index 5820e27..8fc7d84 100644 --- a/resources/public/js/geocsv.js +++ b/resources/public/js/geocsv.js @@ -10,21 +10,26 @@ var GeoCSV = { var record = new Object(); for ( i = 0; i < Math.min( ks.length, vs.length); i++) { + if ( ks[ i]) { record[ ks[ i]] = vs[ i]; + } } return record; }, prepareRecords( data) { - var cols = data[0].forEach( c => { - c.trim().toLowerCase().replace( /[^\w\d]+/, "-"); + var cols = data[0].map( c => { + return c.trim().toLowerCase().replace( /[^\w\d]+/, "-"); }); + console.log( "data[0]: " + data[0] + "; cols: " + cols); + var rest = data.slice( 1); // I should be able to do this with a forEach over data.slice( 1), but // I've failed to make it work. + var result = []; for ( j = 1; j < rest.length; j++) { @@ -71,7 +76,7 @@ var GeoCSV = { popupContent( record) { var c = "
" + record[ "name"] + "
"; - record.keys().foreach( k => { + Object.keys(record).forEach( k => { c += "
" + k + "" + @@ -85,21 +90,26 @@ var GeoCSV = { addPin( record, index, view) { var lat = Number( record[ "latitude"]); var lng = Number( record[ "longitude"]); - var pin = L.icon( {iconAnchor: [16, 41], - iconSize: [32, 42], - iconUrl: "img/map-pins/" + - this.pinImage( record) + - ".png", - riseOnHover: true, - shadowAnchor: [16, 23], - shadowSize: [57, 24], - shadowUrl: "img/map-pins/shadow_pin.png"}); - var marker = L.marker( L.latLng( lat, lng), - {icon: pin, title: record["name"]}); - marker.bindPopup( popupContent( record)); - marker.addTo( view); - return marker; + if ( !isNaN( lat) && !isNaN( lng)) { + var pin = L.icon( {iconAnchor: [16, 41], + iconSize: [32, 42], + iconUrl: "img/map-pins/" + + this.pinImage( record) + + ".png", + riseOnHover: true, + shadowAnchor: [16, 23], + shadowSize: [57, 24], + shadowUrl: "img/map-pins/shadow_pin.png"}); + var marker = L.marker( L.latLng( lat, lng), + {icon: pin, title: record["name"]}); + marker.bindPopup( this.popupContent( record)); + marker.addTo( view); + + return marker; + } else { + return null; + } }, removePins( view) { @@ -143,10 +153,15 @@ var GeoCSV = { }, refreshPins( view, records) { - removePins( view); - record.forEach( r => { - }); - computeBounds( view, records); + this.removePins( view); + + for ( i = 0; i < records.length; i++) { + if( records[i]) { + this.addPin( records[i], i, view); + } + } + + this.computeBounds( view, records); } }, @@ -208,7 +223,7 @@ var GeoCSV = { this.Notify.message( "Found " + records.length + " records of inline data for map " + id); - this.Gis.refreshPins( view, records); + this.GIS.refreshPins( view, records); } } }