Leaflet GetFeatureInfo Request with JSONP
HTML
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.js"></script>
<div id="map"></div>
<br />
<b>Datasource of the WFS-Layer: <a href="http://www.dartmoor.gov.uk" target="_blank">http://www.dartmoor.gov.uk/</a> </b>
<p>
Have a look at the output of console.log to see the response of the GetFeatureInfo Requehttp://jsfiddle.net/expedio/xovLvt0h/#st...
</p>
CSS
html { height: 100% }
body { height: 100%; margin: 0; padding: 0;}
#map{ height: 65% }
JavaScript
var m= L.map('map').setView([42.2, -71], 0);
var mapQuestAttr = 'Tiles Courtesy of <a href="http://www.mapquest.com/">MapQuest</a> — ';
var osmDataAttr = 'Map data © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';
var mopt = {
url: 'http://otile{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpeg',
options: {attribution:mapQuestAttr + osmDataAttr, subdomains:'1234'}
};
var osm = L.tileLayer("http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",{attribution:osmDataAttr});
var mq=L.tileLayer(mopt.url,mopt.options);
osm.addTo(m);
// ---------------------------------------------
// JSONP used for vectorLayer
var rootUrl = 'http://maps.dartmoor.gov.uk/geoserver/ows';
var defaultParameters = {
service: 'WFS',
version: '1.0.0',
request: 'GetFeature',
typeName: 'general:tpo_points',
maxFeatures: 200,
outputFormat: 'text/javascript'
, format_options: 'callback: getJson',
srsName:'EPSG:4326'
};
var parameters = L.Util.extend(defaultParameters);
//console.log(rootUrl + L.Util.getParamString(parameters));
$.ajax({
jsonp : false,
url: rootUrl + L.Util.getParamString(parameters),
dataType: 'jsonp',
jsonpCallback: 'getJson',
success: handleJson
});
var group = new L.featureGroup().addTo(m);
var geojsonlayer;
function handleJson(data) {
console.log("GetFeature-Request: ");
console.log(data);
geojsonlayer=L.geoJson(data, {
onEachFeature: function (feature, my_Layer) {
my_Layer.bindPopup("Ref: "+feature.properties.ref+"<br />Tpo name: "+feature.properties.tpo_name);
},
pointToLayer: function (feature, latlng) {
//return L.circleMarker(latlng, geojsonMarkerOptions);
return L.circleMarker(latlng//, geojsonMarkerOptions
);
//return L.marker(latlng);
}
}).addTo(group);
m.fitBounds(group.getBounds());
}
function getJson(data) {
console.log("callback function fired");
//never
}
//...