For the past few hours, I've been attempting to load a JSON file into JavaScript to feed map coordinates to the Google Maps service.
Unfortunately, my script is incomplete and I cannot obtain any results. Here's what I have so far:
<script>
function loadJSON(callback) {
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', 'http://127.0.0.1/irismaps/aitems.php', true); // path to file
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == "200") {
// Since .open does not return a value in asynchronous mode, use an anonymous callback here
callback(xobj.responseText);
}
};
xobj.send(null);
}
function init() {
loadJSON(function(response) {
// Parse JSON string into object
results = JSON.parse(response);
for (var i=0; i<results[0].markers.length; i++) {
for (var b=0;b<results[0].markers[i].length;b++) {
// The desired object
console.log(results[0].markers[i]);
break;
}
}
});
}
</script>
JSON:
{
"results": [
{
"markers": [
{
"longitude": "37.66653612499999",
"latitude": "55.77875861131171",
"title": "Industry LLC",
"icon": "img/markers/shop.png"
},
...
OMITTED FOR BREVITY
...
{
"longitude": "-115.20111025",
"latitude": "55.80752971122906",
"title": "Book Group Inc.",
"icon": "img/markers/shop.png"
}
]
}
]
}