Thursday 20 August 2015

Adding pop-up to GeoJSON layer in Leaflet


I'm new to working with the Leaflet API and am running into trouble with creating popups for a GeoJSON layer. I've looked at the following post as a reference and am still having difficulty: binding nested arrays as geojson popups in leaflet


My GeoJson data looks like:



{
"type": "FeatureCollection",
"features": [
{
"geometry": {
"type": "Point",
"coordinates": [
-73.97364044189453,
40.66893768310547
]

},
"type": "Feature",
"properties": {
"lon": -73.97364044189453,
"lat": 40.66893768310547,
"version": "1.1",
"t": 1381167616,
"device": "iPhone3,3",
"alt": 67,
"os": "6.1.3"

}
},
{
"geometry": {
"type": "Point",
"coordinates": [
-73.96121215820312,
40.66240692138672
]
},

"type": "Feature",
"properties": {
"lon": -73.96121215820312,
"lat": 40.66240692138672,
"version": "1.1",
"t": 1381171200,
"device": "iPhone3,3",
"alt": 45,
"os": "6.1.3"
}

}

]
}

My leaflet js is as follows:


// create a variable to load Stamen 'toner' tiles
var layer = new L.StamenTileLayer("toner");

// initialize and set map center and zoom

var map = L.map('map', {
center: new L.LatLng(40.67, -73.94),
zoom: 12
});

// create the map
map.addLayer(layer);

// on each feature use feature data to create a pop-up
function onEachFeature(feature, layer) {


if (feature.properties) {

var popupContent;
popupContent = feature.properties.t;

console.log(popupContent);
}
layer.bindPopup(popupContent);
}


// grab the processed GeoJSON through ajax call
var geojsonFeature = (function() {
var json = null;
$.ajax({
'async': false,
'global': false,
'url': "/data/test_random.json",
'dataType': "json",
'success': function (data) {

json = data;
}
});
return json;
})();

// create an object to store marker style properties
var geojsonMarkerOptions = {
radius: 10,
fillColor: "rgb(255,0,195)",

color: "#000",
weight: 0,
opacity: 1,
fillOpacity: 1
};

// load the geojson to the map with marker styling
L.geoJson(geojsonFeature, {

style: function (feature) {

return feature.properties && feature.properties.style;
},

onEachFeature: onEachFeature,

pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, geojsonMarkerOptions)
}
}).addTo(map);


The console.log(popupContent); call within the onEachFeature function is returning data, however when I click on the GeoJSON points in the map I get the following error: Uncaught NotFoundError: An attempt was made to reference a Node in a context where it does not exist.


I've tried looking into this with no success so far.



Answer



Here's an example that I have of loading geojson from a WFS service: http://maps.gcc.tas.gov.au/dogexerciseareas.html


This is another example loading topojson (similar, but different): http://agl.pw/examples/NRM_Regions/map.html


Here's some simple code I use to load a layer:


var myLayer = L.geoJson().addTo(map);
$.getJSON("data/buildings.json", function(json) {
myLayer.addData(json);
});


Then you can do interactivity and styling with something like this:


    success : function (response) {
DogExerciseAreas = L.geoJson(response, {
style: function (feature) {
return {
stroke: false,
fillColor: 'FFFFFF',
fillOpacity: 0
};

},
onEachFeature: function (feature, layer) {
popupOptions = {maxWidth: 200};
layer.bindPopup("Site name: " + feature.properties.sitename +
"
Dog Exercise: " + feature.properties.dog_exercise +
"

Please ensure that tidy up after your dog. Dogs must be kept under effective control at all times."
,popupOptions);
}
}).addTo(map);
}


EDIT: an example from the leaflet website on styling points (from here http://leafletjs.com/examples/geojson.html):


var geojsonMarkerOptions = {
radius: 8,
fillColor: "#ff7800",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
};


L.geoJson(someGeojsonFeature, {
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, geojsonMarkerOptions);
}
}).addTo(map);

EDIT2: Added a solution to this problem. See here: https://gist.github.com/alexgleith/7112515


All you need to do now is edit the bit where it says 'popupContent' to add your bit and change the code to load data from the file.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...