22
loading...
This website collects cookies to deliver better user experience
Goto the Layer Preview tab
In your layer preview, Inside all formats dropdown button, select WFS>GeoJson
Note: Make sure you select the vector layer. Your raster layer doesn't contain any options for WFS.
Jsonp
format in GeoServer. To enable the Jsonp
in your GeoServer you have to follow the following steps;<context-param>
<param-name>ENABLE_JSONP</param-name>
<param-value>true</param-value>
</context-param>
//Geoserver Web Feature Service
$.ajax('http://localhost:8080/geoserver/wfs',{
type: 'GET',
data: {
service: 'WFS',
version: '1.1.0',
request: 'GetFeature',
typename: 'workspace:layer_name',
srsname: 'EPSG:4326',
outputFormat: 'text/javascript',
},
dataType: 'jsonp',
jsonpCallback:'callback:handleJson',
jsonp:'format_options'
});
// the ajax callback function
function handleJson(data) {
selectedArea = L.geoJson(data).addTo(map);
map.fitBounds(selectedArea.getBounds());
}
workspace:layer_to_display
. Also, you can filter (query) the required data by passing CQL_FILTER
inside your data section. If I am interested in the data column name as a column and value as a demo then I can filter this data by adding CQL_FILTER: "column:'demo'"
key-value pair in the data section. Also, don't forget to write outputFormat: 'text/javascript'
in your code. I already mentioned, why we can't fetch the JSON data. Use Jsonp
as the dataType and set a callback function handleJson
.handleJson
a function defined below the ajax call will receive the data from URL and L.GeoJSON
will handle this data through URL and add this data to the leaflet map. If you want to fit the data in a defined bound, you can set it using map.fitBounds(selectedArea.getBounds);
. Then you can add the popup feature and style feature of the GeoJSON. onEachFeature
is used to fetch the properties of the GeoJSON.<html>
<head>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css">
<style>
#map {
width: 100%;
height: 500px;
}
</style>
</head>
<body>
<div id="map"></div>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
</body>
</html>
<script>
var map = L.map("map").setView([38.45, 70.6], 6);
var osm = L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution:
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
//Geoserver Web Feature Service
$.ajax('http://localhost:8080/geoserver/wfs',{
type: 'GET',
data: {
service: 'WFS',
version: '1.1.0',
request: 'GetFeature',
typename: 'workspace:layer_name',
CQL_FILTER: "column='demo'",
srsname: 'EPSG:4326',
outputFormat: 'text/javascript',
},
dataType: 'jsonp',
jsonpCallback:'callback:handleJson',
jsonp:'format_options'
});
//Geojson style file
var myStyle = {
'color': 'red'
}
// the ajax callback function
function handleJson(data) {
selectedArea = L.geoJson(data, {
style: myStyle,
onEachFeature: function(feature, layer) {
layer.bindPopup(`Name: ${feature.properties.name_of_your_property}`)
}
}).addTo(map);
map.fitBounds(selectedArea.getBounds());
}
</script>