ajax根据坐标查询WMS地图服务属性信息
1 <html lang="en"> 2 3 <head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> 6 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 7 <title>demotitle> 8 <style> 9 10 style> 11 12 head> 13 14 <body> 15 <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js">script> 16 <script type="text/javascript" src="jquery.xml2json.js">script> 17 <script> 18 $.ajax({ 19 url: "http://127.0.0.1:6080/arcgis/services/Mymap/MapServer/WMSServer", 20 data: { 21 version: '1.3.0', 22 request: 'getfeatureinfo', 23 layers: '0', 24 styles: '', 25 CRS: 'EPSG:4326', 26 bbox: '28.123462,120.123272,29.481238,120.123941',//xmin,ymin,xmax,ymax,当前地图的范围,arcgis api for js中对应map.extent.ymin,map.extent.xmin,注意顺序 27 width: '1730',//当前地图视图的宽度,arcgis api for js中对应map.width 28 height: '919', 29 info_format: 'text/xml', 30 x: '760',//经纬度坐标要转成屏幕坐标(像素坐标) 31 y: '229', 32 query_layers: "0" 33 }, 34 dataType: 'xml', 35 success: function(data) {
var jsondata=$.xml2json(data);//xml转json 36 console.log(jsondata); 37 }, 38 error: function(e) { 39 console.log(e); 40 } 41 42 }); 43 script> 44 body> 45 46 html>
附上jquery.xml2json源码
1 /* 2 ### jQuery XML to JSON Plugin v1.3 - 2013-02-18 ### 3 * http://www.fyneworks.com/ - diego@fyneworks.com 4 * Licensed under http://en.wikipedia.org/wiki/MIT_License 5 ### 6 Website: http://www.fyneworks.com/jquery/xml-to-json/ 7 */ 8 /* 9 # INSPIRED BY: http://www.terracoder.com/ 10 AND: http://www.thomasfrank.se/xml_to_json.html 11 AND: http://www.kawa.net/works/js/xml/objtree-e.html 12 */ 13 /* 14 This simple script converts XML (document of code) into a JSON object. It is the combination of 2 15 'xml to json' great parsers (see below) which allows for both 'simple' and 'extended' parsing modes. 16 */ 17 // Avoid collisions 18 ; 19 if (window.jQuery)(function($) { 20 21 // Add function to jQuery namespace 22 $.extend({ 23 24 // converts xml documents and xml text to json object 25 xml2json: function(xml, extended) { 26 if (!xml) return {}; // quick fail 27 28 //### PARSER LIBRARY 29 // Core function 30 function parseXML(node, simple) { 31 if (!node) return null; 32 var txt = '', 33 obj = null, 34 att = null; 35 var nt = node.nodeType, 36 nn = jsVar(node.localName || node.nodeName); 37 var nv = node.text || node.nodeValue || ''; 38 /*DBG*/ //if(window.console) console.log(['x2j',nn,nt,nv.length+' bytes']); 39 if (node.childNodes) { 40 if (node.childNodes.length > 0) { 41 /*DBG*/ //if(window.console) console.log(['x2j',nn,'CHILDREN',node.childNodes]); 42 $.each(node.childNodes, function(n, cn) { 43 var cnt = cn.nodeType, 44 cnn = jsVar(cn.localName || cn.nodeName); 45 var cnv = cn.text || cn.nodeValue || ''; 46 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>a',cnn,cnt,cnv]); 47 if (cnt == 8) { 48 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>b',cnn,'COMMENT (ignore)']); 49 return; // ignore comment node 50 } else if (cnt == 3 || cnt == 4 || !cnn) { 51 // ignore white-space in between tags 52 if (cnv.match(/^\s+$/)) { 53 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>c',cnn,'WHITE-SPACE (ignore)']); 54 return; 55 }; 56 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>d',cnn,'TEXT']); 57 txt += cnv.replace(/^\s+/, '').replace(/\s+$/, ''); 58 // make sure we ditch trailing spaces from markup 59 } else { 60 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>e',cnn,'OBJECT']); 61 obj = obj || {}; 62 if (obj[cnn]) { 63 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>f',cnn,'ARRAY']); 64 65 // http://forum.jquery.com/topic/jquery-jquery-xml2json-problems-when-siblings-of-the-same-tagname-only-have-a-textnode-as-a-child 66 if (!obj[cnn].length) obj[cnn] = myArr(obj[cnn]); 67 obj[cnn] = myArr(obj[cnn]); 68 69 obj[cnn][obj[cnn].length] = parseXML(cn, true /* simple */ ); 70 obj[cnn].length = obj[cnn].length; 71 } else { 72 /*DBG*/ //if(window.console) console.log(['x2j',nn,'node>g',cnn,'dig deeper...']); 73 obj[cnn] = parseXML(cn); 74 }; 75 }; 76 }); 77 }; //node.childNodes.length>0 78 }; //node.childNodes 79 if (node.attributes) { 80 if (node.attributes.length > 0) { 81 /*DBG*/ //if(window.console) console.log(['x2j',nn,'ATTRIBUTES',node.attributes]) 82 att = {}; 83 obj = obj || {}; 84 $.each(node.attributes, function(a, at) { 85 var atn = jsVar(at.name), 86 atv = at.value; 87 att[atn] = atv; 88 if (obj[atn]) { 89 /*DBG*/ //if(window.console) console.log(['x2j',nn,'attr>',atn,'ARRAY']); 90 91 // http://forum.jquery.com/topic/jquery-jquery-xml2json-problems-when-siblings-of-the-same-tagname-only-have-a-textnode-as-a-child 92 //if(!obj[atn].length) obj[atn] = myArr(obj[atn]);//[ obj[ atn ] ]; 93 obj[cnn] = myArr(obj[cnn]); 94 95 obj[atn][obj[atn].length] = atv; 96 obj[atn].length = obj[atn].length; 97 } else { 98 /*DBG*/ //if(window.console) console.log(['x2j',nn,'attr>',atn,'TEXT']); 99 obj[atn] = atv; 100 }; 101 }); 102 //obj['attributes'] = att; 103 }; //node.attributes.length>0 104 }; //node.attributes 105 if (obj) { 106 obj = $.extend((txt != '' ? new String(txt) : {}), /* {text:txt},*/ obj || {} /*, att || {}*/ ); 107 //txt = (obj.text) ? (typeof(obj.text)=='object' ? obj.text : [obj.text || '']).concat([txt]) : txt; 108 txt = (obj.text) ? ([obj.text || '']).concat([txt]) : txt; 109 if (txt) obj.text = txt; 110 txt = ''; 111 }; 112 var out = obj || txt; 113 //console.log([extended, simple, out]); 114 if (extended) { 115 if (txt) out = {}; //new String(out); 116 txt = out.text || txt || ''; 117 if (txt) out.text = txt; 118 if (!simple) out = myArr(out); 119 }; 120 return out; 121 }; // parseXML 122 // Core Function End 123 // Utility functions 124 var jsVar = function(s) { return String(s || '').replace(/-/g, "_"); }; 125 126 // NEW isNum function: 01/09/2010 127 // Thanks to Emile Grau, GigaTecnologies S.L., www.gigatransfer.com, www.mygigamail.com 128 function isNum(s) { 129 // based on utility function isNum from xml2json plugin (http://www.fyneworks.com/ - diego@fyneworks.com) 130 // few bugs corrected from original function : 131 // - syntax error : regexp.test(string) instead of string.test(reg) 132 // - regexp modified to accept comma as decimal mark (latin syntax : 25,24 ) 133 // - regexp modified to reject if no number before decimal mark : ".7" is not accepted 134 // - string is "trimmed", allowing to accept space at the beginning and end of string 135 var regexp = /^((-)?([0-9]+)(([\.\,]{0,1})([0-9]+))?$)/ 136 return (typeof s == "number") || regexp.test(String((s && typeof s == "string") ? jQuery.trim(s) : '')); 137 }; 138 // OLD isNum function: (for reference only) 139 //var isNum = function(s){ return (typeof s == "number") || String((s && typeof s == "string") ? s : '').test(/^((-)?([0-9]*)((\.{0,1})([0-9]+))?$)/); }; 140 141 var myArr = function(o) { 142 143 // http://forum.jquery.com/topic/jquery-jquery-xml2json-problems-when-siblings-of-the-same-tagname-only-have-a-textnode-as-a-child 144 //if(!o.length) o = [ o ]; o.length=o.length; 145 if (!$.isArray(o)) o = [o]; 146 o.length = o.length; 147 148 // here is where you can attach additional functionality, such as searching and sorting... 149 return o; 150 }; 151 // Utility functions End 152 //### PARSER LIBRARY END 153 154 // Convert plain text to xml 155 if (typeof xml == 'string') xml = $.text2xml(xml); 156 157 // Quick fail if not xml (or if this is a node) 158 if (!xml.nodeType) return; 159 if (xml.nodeType == 3 || xml.nodeType == 4) return xml.nodeValue; 160 161 // Find xml root node 162 var root = (xml.nodeType == 9) ? xml.documentElement : xml; 163 164 // Convert xml to json 165 var out = parseXML(root, true /* simple */ ); 166 167 // Clean-up memory 168 xml = null; 169 root = null; 170 171 // Send output 172 return out; 173 }, 174 175 // Convert text to XML DOM 176 text2xml: function(str) { 177 // NOTE: I'd like to use jQuery for this, but jQuery makes all tags uppercase 178 //return $(xml)[0]; 179 180 /* prior to jquery 1.9 */ 181 /* 182 var out; 183 try{ 184 var xml = ((!$.support.opacity && !$.support.style))?new ActiveXObject("Microsoft.XMLDOM"):new DOMParser(); 185 xml.async = false; 186 }catch(e){ throw new Error("XML Parser could not be instantiated") }; 187 try{ 188 if((!$.support.opacity && !$.support.style)) out = (xml.loadXML(str))?xml:false; 189 else out = xml.parseFromString(str, "text/xml"); 190 }catch(e){ throw new Error("Error parsing XML string") }; 191 return out; 192 */ 193 194 /* jquery 1.9+ */ 195 return $.parseXML(str); 196 } 197 198 }); // extend $ 199 200 })(jQuery);
我测试的是ArcServer发布的wms服务,geoserver发布的wms服务请自行测试。
附上xml2json代码,感谢原作者xtreemrage,github链接https://github.com/xtreemrage/jquery.xml2json