0

The last three hours i have been trying to read a JSON file into java-script for my map corodinates to be read by google-maps service.

I however,have an incomplete script..i can't get my results.Here is my script:

<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") {
            // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
            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++) {

                    //this is the object you are looking for
                    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"
        },
        {
          "longitude": "-82.28813722500001",
          "latitude": "23.10569357133444",
          "title": "Garden Care",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "-4.792105912500006",
          "latitude": "37.875266800953554",
          "title": "Entertainers",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "-73.94999999999999",
          "latitude": "40.65",
          "title": "Educators Corp.",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "-0.8122895000000199",
          "latitude": "52.0443456",
          "title": "Rentacar",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "4.879416200000037",
          "latitude": "45.7783975",
          "title": "Restaurant Lucia",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "12.481953799999928",
          "latitude": "41.90567180000001",
          "title": "The Airport",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "-74.0059731",
          "latitude": "40.7143528",
          "title": "Advertising Inc.",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "12.7880859375",
          "latitude": "4.565473550710291",
          "title": "Car Repairs",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "13.447265625",
          "latitude": "52.53627304145948",
          "title": "Accounting Gmbh.",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "39.638671875",
          "latitude": "24.447149589730845",
          "title": "Courier & Courier",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "10.7666015625",
          "latitude": "59.93300042374631",
          "title": "Your House A.S.",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "-1.8896484375",
          "latitude": "52.482780222078205",
          "title": "M Supermarkets LLC",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "21.005859375",
          "latitude": "52.22779941887071",
          "title": "Healthy Runner S.A.",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "-0.8122895000000199",
          "latitude": "52.0443456",
          "title": "Rentacar",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "-0.8122895000000199",
          "latitude": "52.0443456",
          "title": "Restaurant Lucia",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "37.6173",
          "latitude": "55.755826",
          "title": "Industrie LLC",
          "icon": "img/markers/shop.png"
        },
        {
          "longitude": "-115.20111025",
          "latitude": "55.80752971122906",
          "title": "Book Group Inc.",
          "icon": "img/markers/shop.png"
        }
      ]
    }
  ]
}

2 Answers2

0

That JSON file you're trying to load is basically just JavaScript. Give your object a name like

var myJSONObject = {
    "foo": "bar"
};

Save it as a .js file and load it into your document.

0

Try:

results.results[0].markers[i]

You are adding the entire file to an object called results that contains an array called results.

Will
  • 3,201
  • 1
  • 19
  • 17