Hello,
I run a non-profit online resource called the Megalithic Portal. I have used OpenLayers in the past but I would like to convert to Leaflet. I
have created a map that uses Leaflet.geoCSV to load in points
from a CSV file
using github.com/joker-x/Leaflet.geoCSV
To see ultimately what I'm hoping to create, I've made a map with
several thousand megalithic sites, based on data from the Megalithic
Portal
http://www.megalithic.co.uk/leaflet_megalith_map-old.html
So far so good but I'm struggling with the feature.properties syntax to get any further
I've created a simpler test map here for debugging purposes:
http://www.megalithic.co.uk/leaflet_megalith_map.html
I can load in the CSV points, and loop around the marker properties as in the code below (var clave) which outputs the CSV data in the marker popups.
What I'm struggling with is how do I refer to the properties individually , eg
popup += 'Colour=' + feature.properties.Colour;
(which is wrong as it shows as 'undefined' in the popup)
I think the CSV loads in as an extension of the GeoJSON format http://leafletjs.com/examples/geojson.html
var bankias = L.geoCsv(null, {
onEachFeature: function (feature, layer) {
var popup = 'SID='+ feature.properties['SID'] +'<br />'; // doesn't work
popup += 'Colour='+ feature.properties.Colour +'<br />'; // doesn't work
for (var clave in feature.properties) {
var title = bankias.getPropertyTitle(clave);
var csvtext = feature.properties[clave];
//if (title != 'SID' && title != 'Colour' && title != 'Icon' && title != 'CatID' && title != 'TypeID') {
popup += '<b>'+title+':</b> '+csvtext+'<br />';
//}
}
layer.bindPopup(popup);
},
pointToLayer: function (feature, latlng) {
return L.marker(latlng, {
icon:L.icon({
//iconUrl: 'images/mapic/' + feature.properties.Icon + '.gif', // WRONG
iconUrl: 'leaflet/images/marcador-bankia.png',
shadowUrl: 'leaflet/images/marker-shadow.png',
iconSize: [25,41],
shadowSize: [41, 41],
shadowAnchor: [13, 20]
})
});
},
firstLineTitles: true
});
The second thing I'd like to do is to display custom markers set by a column in the CSV.
In the properties I have imported a field into the feature.properties called Icon
Icon: tp6 Icon: tg60 etc
I would like to create a different marker for each point based on this property - do I need to do this as part of the onEachFeature and loop around all the markers to set the iconUrl property?
I've tried the following but it doesn't work so I must have the syntax wrong:
iconUrl: 'images/mapic/' + feature.properties.Icon + '.gif'
eg should generate http://www.megalithic.co.uk/images/mapic/tg60.gif http://www.megalithic.co.uk/images/mapic/tg6.gif etc
Does someone have an example of something similar please -
Many thanks if you can help to improve our map resource.
Andy
http://www.megalithic.co.uk/index.php
Line 166 to set the icon GIFs 'just works' on the JSfiddle version but it doesn't in my CSV version (line 126 of that)
feature.properties.icon
features.properties.Iconhttp://www.megalithic.co.uk/cache/csvmap_country59.csv
I checked it by importing into http://geojson.io/ and put the GeoJSON in JSFiddle, where it works
extract:
{
"type": "Feature",
"properties": {
"SID": 12870,
"Name": "Beisenerbierg Menhir",
"Type": "Standing Stone (Menhir)",
"Colour": "Our Photo Page",
"Accuracy": 5,
"Condition": 5,
"Ambience": 4,
"Access": 2,
"TypeID": 21,
"CatID": 1,
"Icon": "tr21"
},
"geometry": {
"type": "Point",
"coordinates": [
6.07602,
49.7592
]
}
},
from http://jsfiddle.net/megalithic/zuz4urb7/