wiesson
wiesson

Reputation: 6832

AMChart SVG Map - Zoom to fit all countries

I'm trying to zoom to all countries that are in my map automatically. It works, if I group the countries with the "groupId" option, but than I also have an unwanted group-hover. I need a single hover/select, but would like to zoom to the countries that they fit in the boundaries.

Here is an example: http://jsfiddle.net/wiesson/efbnsmpd/

Example map data:

dataProvider: {
    map: "worldHigh",
    areas: [{
        "id": "AT",
        "customData": "Austria",
        "link": "austria",
    }, {
        "id": "HR",
        "customData": "Croatia",
        "link": "croatia",
    }, {
        "id": "FR",
        "customData": "France",
        "link": "france",
    }, {
        "id": "DE",
        "customData": "Germany",
        "link": "germany",
    }, {
        "id": "GR",
        "customData": "Greece",
        "link": "greece",
    }, {
        "id": "IT",
        "customData": "Italy",
        "link": "italy",
    }, {
        "id": "MA",
        "customData": "Malta",
        "link": "malta",
    }, {
        "id": "PT",
        "customData": "Portugal",
        "link": "portugal",
    }, {
        "id": "ES",
        "customData": "Spain",
        "link": "spain",
    }, {
        "id": "CH",
        "customData": "Switzerland",
        "link": "switzerland",
    }, {
        "id": "TR",
        "customData": "Turkey",
        "link": "turkey",
    }, {
        "id": "GB",
        "customData": "United Kingdom",
        "link": "united-kingdom"
    }]
}

Do I need to calculate the lat/lng (somehow) based on my countries and then zoom? (e.g. http://docs.amcharts.com/3/javascriptmaps/AmMap#zoomTo(zoomLevel, zoomX, zoomY, instantly))

Upvotes: 1

Views: 1455

Answers (2)

wiesson
wiesson

Reputation: 6832

I've figured out a working solution by myself, as explained here: http://jsfiddle.net/f1Ljtawm/embedded/result,js,html,css/

map.addListener('init', function () {
    map.zoomToGroup("zoomed-group");

    function toggleMapObjectAlpha(e) {
        var alpha = e.type == "rollOverMapObject" ? .3 : .8;
        e.event.target.setAttribute("fill-opacity", alpha);
    }

    map.addListener('rollOverMapObject', toggleMapObjectAlpha);
    map.addListener('rollOutMapObject', toggleMapObjectAlpha);
 });

Upvotes: 0

martynasma
martynasma

Reputation: 8595

There's probably a better way. The zoomToGroup function can accept an array of area objects (not ids but the real objects). So you could do it like this:

map.addListener("init", function (event) {
    var zoomToAreasIds = ['AT', 'HR', 'FR', 'DE', 'GR', 'ID', 'MA', 'PT', 'ES', 'CH', 'TR', 'GB'];
    var zoomToAreas = [];
    var area;
    for(var i = 0; i < zoomToAreasIds.length; i++) {
        if (area = map.getObjectById(zoomToAreasIds[i]))
            zoomToAreas.push(area);
    }
    map.zoomToGroup(zoomToAreas);
});

Here's your updated fiddle:

http://jsfiddle.net/amcharts/efbnsmpd/6/

Upvotes: 3

Related Questions