Still have questions?
Contact support

AJAX tutorial

Include AnyMap component

First of all, you need the AnyMap component, which can be found on the download page. Reference the AnyMap JavaScript file in the section of your web page.

<script src="//" type="text/javascript"></script> 

If you need any other AnyChart components, it's better to include anychart-bundle.min.js, which can be found on the same page.

AJAX and Maps

To create a sample with maps being added through AJAX request, we need to do is to include some AJAX component to our Drill Down Map sample (in the same <head> section). We will use jQuery.

<script src=""></script>

It is better to add the root map the same way as the AnyMap component and jQuery:

<script src=""></script>


Let's create a data set for the current map of the USA and for maps of Floorida and Texas, which we're going to load later using AJAX. We have used data about population in those states in 2000 for this sample.

As usual, we can define the data in array of arrays or in array of objects format. In the next sample we've set the data as array of objects.

// set the data for the USA map
var dataSetUSA =[
{"id": "US.TX", "value": 26956958},
{"id": "US.FL", "value": 19552860}

// create data set for Texas
var dataSetTX =[
{'id': 'US.TX.111', 'value': 6222}, // Dallam
{'id': 'US.TX.421', 'value': 3186}, // Sherman
{'id': 'US.TX.195', 'value': 5369}, // Hansford
{'id': 'US.TX.357', 'value': 9006}, // Ochiltree
{'id': 'US.TX.295', 'value': 3057}, // Lipscomb

// create data set for Florida 
var dataSetFL =[
{'id': 'US.FL.063', 'value': 46755}, //Jackson
{'id': 'US.FL.091', 'value': 170498}, //Okaloosa
{'id': 'US.FL.077', 'value': 7021}, //Liberty County
{'id': 'US.FL.079', 'value': 18733}, //Madison

After we have defined the data, it's time to create series.

AnyMap provides several types of series, so at the first step you should make up your mind about which series (map) type (or types) you are going to use in your Drill Down Map.

In this sample we've decided to use Choropleth Map, as it's one of the most popular series type.

// create series for USA map
usaSeries = usaMap.choropleth(usaDataSet);

Now, it's time to enable the drill down interactivity in our map.

Load maps using AJAX

Using AJAX means that you don't load all maps you're going to use in your Drill Down Map simultaneously - you loading a map time when needed.

In jQuery AJAX request should contain following fields:

  • type: "GET" and "POST",
    • url: a link to the requested file,
    • dateType: a type of the data of the requested file ("json" when you load map in GeoJSON format),
    • success: in this field we set a function which is to be run when the requested file has been successfully loaded,
    • error: this field contains actions that should be performed if the request fails (this field is not necessary). In our case it shows an alert, warning a user of failed map load.
type: "GET", 
url: "//", 
dataType: "json",
success: function(data){
error: function(){
    	alert("Map loading failed");

You can find links to all maps in AnyChart Map Collection page.

Drill To

To drill to a map we use the drillTo() method, which requires the id of the map and the map name.

So, when AJAX request is successfully performed, we create a map, pass the data that we got into the new map's geoData, then create a series and use a drillTo method to load the map of the selected region and perform the drill down. So the "success" field of the AJAX request will look like the following:

success: function(data){
var txMap =;
txSeries = txMap.choropleth(dataSetTX); 
usaMap.drillTo("US.TX", txMap);  

To load the map of the higher level, or simply to drill up, press "Esc" button. Find more about special methods of drill down in the Methods article.

JsFiddle Playground

Dynamic URL

It's rather inconvenient to write a new AJAX request for each state. Let's change the AJAX-request a bit to let it use different URLs depending of the state we have selected.

Let's create a function that peforms AJAX requests, it will have three parameters: the id of the clicked region, the url corresponding to the clicked regions and the root map. This function will be called on the "pointclick" event.

We need to add a field with url to both states in the dataset of the USA map:

// set the data for the USA map
var usaDataSet =[
{"id": "US.TX", "value": 26956958, "url": ""},
{"id": "US.FL", "value": 19552860, "url": ""}

// load the map using AJAX
usaSeries.listen("pointClick", function(e) {
loadMap(e.point.get("id"), e.point.get("url"), usaMap);      

function loadMap(id, url, map){
type: "GET",
url: url, 
dataType: "json", 
success: function(data){              
              // create a map to load
              var drillMap =;
              // put the data into the map
              // perform a drill down into the drillMap
              map.drillTo(id, drillMap);
              var drillMapSeries = drillMap.choropleth(dataSets[id]);
error: function(){
            alert('Error: ', data);

JsFiddle Playground

To open a map of a previous level press "Esc" button.

Explore the sample to see the full source code.

Adjust settings

The sample demonstrated in this article uses AJAX request to load maps. If you prefer to reference the maps directly, check out the Basic Tutorial article. For other drill down methods look into the Methods article.