0

I would like to customize leaflet .bindPoPup geoJSON marker. I found some solutions, although I would like to use my existing one.

Basically the operation leads to merge two functions as per below:

var sitec = {
    "type": "FeatureCollection",
        "features": [
            {
             "type": "Feature",
             "properties": {
                 "Title": "Sitec IS",
                 "Head": "7400 Beach Drive",
                 "Description": "Gavin Sinclair"
        },
            "geometry": {
            "type": "Point",
            "coordinates": [
            0.16964435577392578,
            52.29220753602784
                ]
                  }
                    },
{
  "type": "Feature",
  "properties": {
"Title": "JHG",
    "Head": "Shortstanton sidings",
"Description": "Conor Murphy"
},
  "geometry": {
    "type": "Point",
    "coordinates": [
      0.05458831787109375,
      52.29163006501503
    ]
  }
}
              ]
        };

 //L.geoJSON(sitec).addTo(map);


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

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

L.geoJSON(sitec, {
    onEachFeature: function (feature, 
   latlng, layer) {
        layer.bindPopup('<h1><u><font 


color="red">'+feature.properties.Title+'</h1> </u></font><h2>Address: '+feature.properties.Head+'</h2><p>'+feature.properties.Description+'</p>');
return L.circleMarker(latlng, geojsonMarkerOptions);
}

    }).addTo(map);

If I merge these two functions I got nothing. If I leave them both I have a custom marker, but a .bindPopup remains the same as per in the image below:

enter image description here

Could you help me sort this out?

2

Usually, marker is defined in pointToLayer and don't need to be returned in onEachFeature. Also onEachFeature takes (feature, layer) as parameter whereas you are passing (feature, latlng, layer) so there might be error. Try this code.

L.geoJSON(sitec, {
    pointToLayer: function (feature, latlng) {
        return L.circleMarker(latlng, geojsonMarkerOptions);
        },
    onEachFeature: function (feature, layer) {
        layer.bindPopup('<h1><u><font color="red">'+feature.properties.Title+'</h1> </u></font><h2>Address: '+feature.properties.Head+'</h2><p>'+feature.properties.Description+'</p>');
    }
}).addTo(map);
0

This will also work: I incorporated the pointToLayer options into the circleMarker without using a separate GeoJSON options.

<!DOCTYPE html>
<html>
<head>
    <title>JSON - Leaflet</title>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css"
          integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
          crossorigin="" />
    <script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js"
            integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
            crossorigin=""></script>
    <style>
        #map {
            width: 800px;
            height: 600px;
        }
    </style>

</head>
<body>
<div id='map'></div>
<script>

var map = L.map('map').setView([52.3059,0.14007], 10); 

var osm=new L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png',{ 
                attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'}).addTo(map);
var sitec = {
    "type": "FeatureCollection",
        "features": [
            {
             "type": "Feature",
             "properties": {
                 "Title": "Sitec IS",
                 "Head": "7400 Beach Drive",
                 "Description": "Gavin Sinclair"
        },
            "geometry": {
            "type": "Point",
            "coordinates": [
            0.16964435577392578,
            52.29220753602784
                ]
                  }
                    },
{
  "type": "Feature",
  "properties": {
"Title": "JHG",
    "Head": "Shortstanton sidings",
"Description": "Conor Murphy"
},
  "geometry": {
    "type": "Point",
    "coordinates": [
      0.05458831787109375,
      52.29163006501503
    ]
  }
}
              ]
        };

function forEachFeature(feature, layer) {  

        var popupContent = '<h1><u><font color="red">'+feature.properties.Title+'</h1> </u></font><h2>Address: '+feature.properties.Head+'</h2><p>'+feature.properties.Description+'</p>'

        layer.bindPopup(popupContent);
}

L.geoJSON(sitec,{onEachFeature:forEachFeature,
    pointToLayer: function (feature, latlng) {
        return L.circleMarker(latlng, {
            radius: 8,
            fillColor: "#ff7800",
            color: "#000",
            weight: 1,
            opacity: 1,
            fillOpacity: 0.8
        })

    }}).addTo(map);

</script>
</body>
</html>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.