Loading external map data

IMPORTANT NOTICE! The information provided in this article is here for historical reference. We strongly suggest you consider using our official Data Loader plugin, which takes care of all the data loading automatically. However, bits and pieces of this article might still be useful for some. Please refer to this tutorial for further information.

This is a very short tutorial, as this sample speaks for itself.  To illustrate how external data can be loaded for the map and how it can be updated in real time, we prepared two files: dataA.txt and dataB.txt. In real life these files most likely won't be a txt files but php or some other script which would get data from database and prepare a valid JSON, accepted by amMap. If you analyse the files, you will notice that the structure of them is exactly the same as of any amMap demo which does not load external data but has it inline. So this is the main thing for you to know. Of course, you might have data in different format, but then you should process the data after loading and make it look like the one in A or B samples.

In this demo data is loaded when "load data A" or "load data B" links are pressed. To load the data we use this method:

function loadData(file){
    var request;
    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
		// load
	    request.open('GET', file, false);
	    request.send();
	    setData(request.responseText);
    }
}

And when it's loaded we simply parse the JSON string, pass it to the map as dataProvider and validate the map:

function setData(data){
	map.dataProvider = JSON.parse(data);
	map.validateNow();
}

That's it!