AMChars,使用外部按钮缩放到某些国家/地区

时间:2017-01-28 15:56:10

标签: javascript zoom amcharts external-links

好的,所以我在AMCharts中有这个地图,我有外部按钮,包含ID,LAT和某些国家的Long,当我将鼠标悬停在它们上面时,我希望地图可以放大并居中到那个国家,这里&#39 ;代码示例......

http://codepen.io/sheko_elanteko/pen/rjYvgE

var map = AmCharts.makeChart("chartdiv", {
  "type": "map",
  "dataProvider": {
    "map": "worldLow",
    "getAreasFromMap": true,
    "areas": [ {
        "id": "EG",
        "color": "#67b7dc",
        "rollOverOutlineColor": "#000",
        "autoZoom": true,
        "balloonText": "Egypt"
      }, {
        "id": "KZ",
        "color": "#67b7dc",
        "rollOverOutlineColor": "#000",
        "autoZoom": true,
        "balloonText": "Kazakistan"
      }, {
        "id": "US",
        "color": "#67b7dc",
        "rollOverOutlineColor": "#000",
        "autoZoom": true,
        "balloonText": "United States"
      }]
  },
  "areasSettings": {
    "color": "#999",
    "autoZoom": false,
    // "selectedColor": "#CC0000",
    "balloonText": "",
    "outlineColor": "#fff",
    "rollOverOutlineColor": "#fff"
  },
  "listeners": [{
    "event": "rendered",
    "method": function(e) {
      // Let's log initial zoom settings (for home button)
      var map = e.chart;
      map.initialZoomLevel = map.zoomLevel();
      map.initialZoomLatitude = map.zoomLatitude();
      map.initialZoomLongitude = map.zoomLongitude();
    }
  }]
});

function centerMap() {
  map.zoomToLongLat(map.initialZoomLevel, map.initialZoomLongitude, map.initialZoomLatitude);
}

$("button.country").mouseenter(function(event) {
  event.stopPropagation();
  var countryID = $(this).data("id");
  var country = map.getObjectById(countryID);
  var lat = $(this).data("lat");
  var long = $(this).data("long");
  country.color = '#000';
  // map.zoomToSelectedObject(country);
  map.zoomToLongLat(3, lat, long);
  country.validate();

}).mouseleave(function(event){
  event.stopPropagation();
  var countryID = $(this).data("id");
  var country = map.getObjectById(countryID);
  country.color = '#CC0000';
  country.validate();
  centerMap();
});

正如您所看到的,我对每个国家/地区的坐标进行了硬编码,以及缩放级别,但缩放功能无法正常工作,尤其是对于美国!!

我也想知道是否有办法获得每个国家的LAT和Long,以便我不会对它们进行硬编码。

在AMCharts中有一个叫做" zoomToSelectedObject"的函数,但是我尝试了这个并给了它没有运气的对象。

1 个答案:

答案 0 :(得分:2)

您可以尝试使用amCharts'selectObject方法。它的默认功能是缩放到提供的地图对象(使用getObjectById已经获得的地图对象。例如:

$("button.country").mouseenter(function(event) {

  event.stopPropagation();
  var countryID = $(this).data("id");
  var country = map.getObjectById(countryID);
  map.selectObject(country);

}).mouseleave( ... );

只需确保将"selectable": true添加到areasSettings即可。没有它,selectObject方法将无法工作。

amCharts地图有很好的文档,你可以在这里找到:https://docs.amcharts.com/3/javascriptmaps/AmMap#selectObject