2014-07-17 54 views
1

我正在嘗試OpenLayers3,並且遇到getfeatureinfo請求時遇到困難。我試圖通過ajax來完成,並將響應轉換爲GML層,就像在OpenLayers2中一樣。將ajax響應轉換爲ol3 GML層

我正在使用的代碼如下:

$.ajax({url: GeoServerURL, 
    data: params, 
    type: 'POST', 
    dataType: 'text', 
    success: function (response) { 
     console.log('Success!'); 
     var gmlLayer = new ol.format.GML({ 
      featureNS: 'http://www.swansea.gov.uk/', 
      featurePrefix: 'ccs', 
      geometryName: 'geom', 
      extractAttributes: true 
     }); 
     var results = gmlLayer.writeFeatures(response.responseText); 
     if (results.length === 0) { 
      return; 
     } 
    } 
}); 

我知道,阿賈克斯給了我正確的響應和2的OpenLayers我會用命令:

results = gmlLayer.read(response.responseText); 

這是我從getFeatures請求返回的內容:

<?xml version="1.0" encoding="UTF-8"?> 
<wfs:FeatureCollection 
    xmlns="http://www.opengis.net/wfs" 
    xmlns:wfs="http://www.opengis.net/wfs" 
    xmlns:ccs="http://www.swansea.gov.uk" 
    xmlns:gml="http://www.opengis.net/gml" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.opengis.net/wfs http://geoserver:8080/geoserver/schemas/wfs/1.0.0/WFS-basic.xsd http://www.swansea.gov.uk http://geoserver:8080/geoserver/wfs?service=WFS&amp;version=1.0.0&amp;request=DescribeFeatureType&amp;typeName=ccs%3Aadopted_roads"> 
     <gml:boundedBy> 
      <gml:Box srsName="http://www.opengis.net/gml/srs/epsg.xml#27700"> 
       <gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs="," ts=" "> 
        270089.11056331,200461.58772218 270347.13150541,200552.92157813 
       </gml:coordinates> 
      </gml:Box> 
     </gml:boundedBy> 
     <gml:featureMember> 
      <ccs:adopted_roads fid="adopted_roads.277"> 
       <gml:boundedBy> 
        <gml:Box srsName="http://www.opengis.net/gml/srs/epsg.xml#27700"> 
         <gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs="," ts=" "> 
          270089.11056331,200461.58772218 270347.13150541,200552.92157813 
         </gml:coordinates> 
        </gml:Box> 
       </gml:boundedBy> 
       <ccs:map_no> 
        sn7000    
       </ccs:map_no> 
       <ccs:text>                                                   
       </ccs:text> 
       <ccs:date_of_adoption>      
       </ccs:date_of_adoption> 
       <ccs:photo>  
       </ccs:photo> 
       <ccs:gid> 
        277 
       </ccs:gid> 
       <ccs:geom> 
        <gml:Polygon srsName="http://www.opengis.net/gml/srs/epsg.xml#27700"> 
         <gml:outerBoundaryIs> 
          <gml:LinearRing> 
           <gml:coordinates xmlns:gml="http://www.opengis.net/gml" decimal="." cs="," ts=" "> 
            270347.13150541,200511.81334524 270345.96895179,200512.03328229 270344.54255622,200511.9333109 270343.33053224,200511.71337386 270340.23863433,200510.91360279 270337.84756661,200509.77392902 270335.37404828,200507.98444126 270330.08896411,200505.44516811 270329.36339874,200504.63539991 270328.26680561,200503.26579195 270325.94169838,200500.78650164 270324.45758738,200499.2369452 270321.40691477,200495.96788096 270313.44218575,200487.21038777 270309.64121258,200483.05157821 270306.49159924,200479.40262271 270301.6682385,200473.55429678 270298.64230107,200470.08528977 270296.72120184,200468.26581059 270292.31833921,200464.00702966 270290.79300291,200462.78737878 270288.72349257,200461.93762202 270286.92606925,200461.58772218 270285.91192673,200461.61771359 270283.74347566,200461.9576163 270283.35595779,200461.99760485 270281.70694557,200462.6874074 270279.65392536,200463.65712981 270261.8610835,200472.52459153 270260.59134409,200473.20439694 270252.4287336,200473.62427675 270252.1319114,200473.42433398 270240.30849378,200473.37434829 270238.22249332,200474.0541537 270237.4227224,200472.15469741 270235.61705402,200472.65455433 270233.5228085,200472.35464018 270228.51805641,200470.55515528 270219.42375401,200475.2538103 270217.21407764,200477.03330093 270211.26938859,200480.25237947 270204.08794037,200486.07071399 270200.89710172,200488.10013308 270194.69681577,200498.39718558 270176.20314372,200513.61283014 270174.00171241,200511.61340247 270157.14056246,200522.86018312 270148.39255263,200528.24864069 270139.18281938,200533.63709826 270137.58327752,200534.7167892 270133.38654142,200536.48628268 270126.42770985,200538.71564454 270118.99066474,200540.70507507 270112.65845782,200541.90473167 270104.19902513,200545.26377016 270101.60183088,200545.26377015 270100.20841555,200545.53369289 270096.01167945,200545.33375012 270089.11056331,200540.91501497 270091.08937798,200550.74220197 270094.14005058,200551.94185857 270103.38276408,200552.32174983 270103.77852701,200552.92157813 270114.67849779,200552.12180706 270123.97892671,200550.02240801 270126.74102218,200549.46256826 270136.7835066,200546.92329512 270143.0579981,200544.7539161 270145.96850466,200543.25434535 270146.9991373,200543.00441689 270154.9061509,200537.10610526 270180.06183232,200520.26092715 270188.10901195,200514.81248675 270216.34834622,200490.5294377 270219.67935091,200488.76994135 270232.79724312,200484.86106025 270236.65593172,200483.41147519 270239.97869134,200481.81193306 270243.35092133,200480.78222781 270246.55000504,200480.28237089 270248.94931782,200479.68254259 270249.74908874,200479.62255976 270256.14725616,200479.58257121 270257.35103508,200479.98245674 270258.95057693,200480.48231365 270261.34988971,200480.78222781 270262.1166804,200480.34235372 270262.79277541,200479.70253686 270265.49715545,200476.98331523 270279.73637597,200469.38549009 270281.70694557,200468.26581059 270283.8836417,200467.41605384 270285.5161638,200466.87620836 270286.57977668,200466.74624557 270287.15693096,200466.68626273 270287.62689944,200466.79623126 270290.91667882,200468.48574764 270291.42787261,200469.14555877 270291.65873432,200469.59543 270292.59867129,200470.68511807 270302.7483415,200481.83192733 270308.78372623,200488.43003864 270313.2195691,200493.2686536 270313.58235179,200493.9984447 270316.66600464,200497.66739448 270319.79912786,200501.39632708 270322.58595851,200504.38547145 270324.6224886,200506.6148333 270328.10190439,200510.40374873 270326.61779339,200514.65253253 270341.45890337,200521.41059806 270347.13150541,200511.81334524 
          </gml:coordinates> 
          </gml:LinearRing> 
         </gml:outerBoundaryIs> 
        </gml:Polygon> 
       </ccs:geom> 
     </ccs:adopted_roads> 
    </gml:featureMember> 
</wfs:FeatureCollection> 

我想要做的是讀取響應然後將其寫入彈出窗口,但我希望將響應作爲GML功能。

+0

您是否正在嘗試讀取或寫入GML,因爲您在OL3示例中有寫入功能並在OL2中讀取? –

+0

此外,如果您可以發佈從Geoserver WFS請求返回的內容,將會有所幫助。 –

+0

謝謝我已經添加了ajax respose,我想我正在嘗試將respose寫入GML特性,然後閱讀它。 –

回答

0

如果服務器是Geoserver(我假設它是真的),您可以將&outputFormat=application/json添加到網址並在GeoJson中獲得答案。

+0

感謝將返回的格式更改爲json作品。我將用結果更新我的代碼。 –

1

這是如何將請求用json格式化爲返回格式。這在ol3中運行良好。

map.on('singleclick', function(evt) { 

    bounds = map.getView().calculateExtent(map.getSize()) 
    var params = { 
     request: "GetFeatureInfo", 
     exceptions: "application/vnd.ogc.se_xml", 
     bbox: bounds.join(','), 
     x: parseInt(evt.pixel[0], 10), 
     y: parseInt(evt.pixel[1], 10), 
     info_format: 'application/json', 
     query_layers: getLayers(), 
     feature_count: 70, 
     buffer: 5, 
     srs: 'EPSG:27700', 
     layers: getLayers(), 
     styles: '', 
     width: map.getSize()[0], 
     height: map.getSize()[1] 
    }; 
    $.ajax({url: GeoServerURL, 
     data: params, 
     type: 'POST', 
     dataType: 'json', 
     success: function (response) { 
      //I couldn't get the response to be read as a GML file so changed it to geoJSON 
      Results = response; 
      if (response.features.length === 0) { 
       return; 
      } 

      setPopup(response,evt,0); 
     } 
    }); 
});