关于OpenLayers的WFS地名点查询中文问题的解决

1. 做个响应按钮:

 <input type=”submit” value=”查询” id=”query” onclick=”query()”/>

2. 写JS代码:

(1) get方式请求:

function query(){

 var str=”碧桂园小区翠林环玉苑46栋03号”;
   new OpenLayers.Request.GET( {
         url : “http://localhost:8080/geoserver/ows?service=WFS&request=GetFeature“,
          params: {
            typeName: “cite:tz_zd_WGS84”,
   filter:”<Filter><PropertyIsEqualTo><PropertyName>TDZL</PropertyName><Value>”+encodeURI(encodeURI(str))+”</Value></PropertyIsEqualTo></Filter>”
        },
         callback : handler
       });
    }
 function handler(req){
  var GML = req.responseXML;
     alert(GML.getElementsByTagName(“cite:TDZL”)[0].childNodes[0].nodeValue); 
 }

无法解决中文传输的问题

 

(2)post方式请求:

function query(){
 var xmlPara = “<?xml version=’1.0′ encoding=’UTF-8′?>”
            + “<wfs:GetFeature  service=’WFS’ version=’1.0.0′ “
            + “xmlns:wfs=’http://www.opengis.net/wfs’ “
            + “xmlns:gml=’http://www.opengis.net/gml’ “
            + “xmlns:ogc=’http://www.opengis.net/ogc’ “
            + “xmlns:xsi=’http://www.w3.org/2001/XMLSchema-instance’ “
            + “xsi:schemaLocation=’http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/wfs.xsd’>”

            + “<wfs:Query typeName=’cite:tz_zd_WGS84′ srsName=’EPSG:4326′>”
            + “<ogc:Filter>”
            + “<ogc:PropertyIsEqualTo>”
            + “<ogc:PropertyName>TDZL</ogc:PropertyName>”
            + “<ogc:Literal>碧桂园小区翠林环玉苑46栋03号</ogc:Literal>”
            + “</ogc:PropertyIsEqualTo>”
            + “</ogc:Filter>”

           + “</wfs:Query>”
           + “</wfs:GetFeature>”;

 
var request = OpenLayers.Request.POST( {
        url : “http://localhost:8080/geoserver/wfs?”,
        data : xmlPara,
        callback : handler
    });
}
function handler(req){
  var GML = req.responseXML;
     alert(GML.getElementsByTagName(“cite:TDZL”)[0].childNodes[0].nodeValue); 
 }

这时服务器端就可以传回“碧桂园小区翠林环玉苑46栋03号”,表示查询成功。

 3. 一般我们用到模糊查询会更多一点,这里只需要做一些改动

var str = txt.value;  //这里接受一个输入框的信息

   ….

    + “<ogc:Filter>”
    + “<ogc:PropertyIsLike wildCard=’*’ singleChar=’.’ escape=’!’>”
    + “<ogc:PropertyName>TDZL</ogc:PropertyName>”
    + “<ogc:Literal>*”+str+”*</ogc:Literal>”
    + “</ogc:PropertyIsLike>”
    + “</ogc:Filter>”

  ….

这里的“*”表示前后都有数据,这样可以做到更人性化了。

转载自:https://blog.csdn.net/zhang88lei/article/details/6590638

You may also like...