1 //參數字段 2 var wfsParams = { 3 service : 'WFS', 4 version : '1.0.0', 5 request : 'GetFeature', 6 typeName : 'cite:bou2_4p', //圖層名稱 7 outputFormat : 'text/javascript', //重點,不要改變 8 format_options : 'callback:loadFeatures' //回調函數聲明 9 }; 10 11 var vectorSource = new ol.source.Vector({ 12 format: new ol.format.GeoJSON(), 13 loader: function(extent, resolution, projection) { //加載函數 14 var url = 'http://localhost:8080/geoserver/wfs'; 15 $.ajax({ 16 url: url, 17 data : $.param(wfsParams), //傳參 18 type : 'GET', 19 dataType: 'jsonp', //解決跨域的關鍵 20 jsonpCallback:'loadFeatures' //回調 21 22 }); 23 }, 24 strategy: ol.loadingstrategy.tile(new ol.tilegrid.createXYZ({ 25 maxZoom: 25 26 })), 27 projection: 'EPSG:4326' 28 }); 29 //回調函數使用 30 window.loadFeatures = function(response) { 31 vectorSource.addFeatures((new ol.format.GeoJSON()).readFeatures(response)); //載入要素 32 33 }; 34 var vectorLayer = new ol.layer.Vector({ 35 source: vectorSource 36 });