我正在使用ESRI在SharePoint 2010中对地址进行地理编码。我能够在console.log中获取地址的响应文本,但我无法弄清楚如何解析x和y来自候选人。有两个目标:1)解析候选[0] x和y,2)如果可能,在地图上映射其他可能的候选(显示它们),这样用户就可以看到另一个点是否真正意味着什么。
这是我的json输出:
{"spatialReference":{"wkid":4326,"latestWkid":4326},"candidates":[{"address":"Detroit, Michigan, United States","location":{"x":-83.04574875899965,"y":42.3314259010005},"score":100,"attributes":{},"extent":{"xmin":-83.188749999999999,"ymin":42.188426,"xmax":-82.902749999999997,"ymax":42.474426000000001}}]}
这是我的javascript:
var request;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
console.log('url used:'+url);
request.open('GET', url);
request.onreadystatechange = function() {
if ((request.readyState===4) && (request.status===200)) {
console.log('response: '+request.responseText);
var data = JSON.parse(request.responseText);
for (var key in data) {
console.log(data[key].value);
}
//var lat = data.spatialReference.candidates[0].address.x;
//console.log('lat:'+lat);
//var items = JSON.parse(request.responseText);
//var output = '<ul>';
//for (var key in items) {
// output += '<li>' + items[key].name + '</li>';
//}
//output += '</ul>';
//document.getElementById('update').innerHTML = output;
}
}
request.send();
答案 0 :(得分:0)
你应该查看每个data.canditates中的location对象,其中包含你需要的信息。
for (var i=0; i<= data.candidates.length; i++) {
cand = data.candidates[i];
if (cand) {
console.log(cand.location);
console.log("x- " + cand.location.x);
console.log("y- " + cand.location.y);
}
}