I'm using ESRI to geocode an address in SharePoint 2010. I'm able to get the response text of the address in the console.log, but I'm not able to figure out how to parse the x and y from the candidates. There are two goals: 1) Parse out the candidate[0] x and y, 2) If possible, map other possible candidates (show them) on a map so the user can see if another point is what they really meant.
Here is my json output:
{"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}}]}
Here is my 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();