[GIS] Leaflet popup options with GeoJSON data

geojsonleafletpopup

In the following how would I customize the popup options? I want to add in the option for the keepInView to be true.

var myLayer = L.geoJson(polygon, {
  onEachFeature: yourOnEachFeatureFunction
}).addTo(map);

function yourOnEachFeatureFunction(feature, layer){
  if (feature.properties.name) {
      layer.bindPopup(feature.properties.name);
  }
}

Best Answer

Check out the Leaflet Documentation.

Here's the bit you want:

L.popup( <Popup options> options?, <ILayer> source? )

You can implement like this:

//set up a standalone popup (use a popup as a layer)
var popup = L.popup({keepInView:true})
     .setLatLng([51.5, -0.09])
     .setContent("I am a standalone popup.")
     .openOn(map);

Or in your example:

function yourOnEachFeatureFunction(feature, layer){ if (feature.properties.name) { layer.bindPopup(feature.properties.name,{keepInView:true}); } }

(This is passing in an object, which is composed of 'attribute:value' pairs.