javascript - amCharts map 在页面刷新时未加载圆圈

标签 javascript html json amcharts

我在使用 amCharts map 时遇到问题,每次刷新页面时我的圆圈都会消失,但我可以放大并显示圆圈。

我不确定该怎么做,是否是因为我公司使用或不使用这种不稳定的“自定义”CMS。我试过在我自己的网站上运行它,但它仍然在做同样的事情。我对 amCharts 和 javascript 比较陌生。

<!-- Resources -->
<script src="https://www.amcharts.com/lib/4/core.js"></script>
<script src="https://www.amcharts.com/lib/4/maps.js"></script>
<script src="https://www.amcharts.com/lib/4/geodata/worldLow.js"></script>
<script src="https://www.amcharts.com/lib/4/themes/animated.js"></script>

<!-- Chart code -->
<script>
// Themes begin
am4core.useTheme(am4themes_animated);
// Themes end

// Create map instance
var chart = am4core.create("chartdiv", am4maps.MapChart);

// Set map definition
chart.geodata = am4geodata_worldLow;

// Set projection
chart.projection = new am4maps.projections.Miller();

// Create map polygon series
var polygonSeries = chart.series.push(new am4maps.MapPolygonSeries());

// Exclude Antartica
polygonSeries.exclude = ["AQ"];

// Make map load polygon (like country names) data from GeoJSON
polygonSeries.useGeodata = true;

// Configure series
var polygonTemplate = polygonSeries.mapPolygons.template;
polygonTemplate.tooltipText = "{name}";
polygonTemplate.fill = am4core.color("#F0F0F0");

// Create hover state and set alternative fill color
var hs = polygonTemplate.states.create("hover");
hs.properties.fill = am4core.color("#cccccc");

// Zoom control
chart.zoomControl = new am4maps.ZoomControl();

// Add image series
var imageSeries = chart.series.push(new am4maps.MapImageSeries());
imageSeries.mapImages.template.propertyFields.longitude = "longitude";
imageSeries.mapImages.template.propertyFields.latitude = "latitude";
imageSeries.mapImages.template.propertyFields.url = "url";
imageSeries.data = [ {
...

// add events to recalculate map position when the map is moved or zoomed
chart.events.on( "mappositionchanged", updateCustomMarkers );

// this function will take current images on the map and create HTML elements for them
function updateCustomMarkers( event ) {

  // go through all of the images
  imageSeries.mapImages.each(function(image) {
    // check if it has corresponding HTML element
    if (!image.dummyData || !image.dummyData.externalElement) {
      // create onex
      image.dummyData = {
        externalElement: createCustomMarker(image)
      };
    }

    // reposition the element accoridng to coordinates
    var xy = chart.geoPointToSVG( { longitude: image.longitude, latitude: image.latitude } );
    image.dummyData.externalElement.style.top = xy.y + 'px';
    image.dummyData.externalElement.style.left = xy.x + 'px';
  });

}

// this function creates and returns a new marker element
function createCustomMarker( image ) {

  var chart = image.dataItem.component.chart;

  // create holder
  var holder = document.createElement( 'div' );
  holder.className = 'map-marker';
  holder.title = image.dataItem.dataContext.title;
  holder.style.position = 'absolute';

  // maybe add a link to it?
  if ( undefined != image.url ) {
    holder.onclick = function() {
      window.location.href = image.url;
    };
    holder.className += ' map-clickable';
  }

  // create dot
  var dot = document.createElement( 'div' );
  dot.className = 'dot';
  holder.appendChild( dot );

  // create pulse
  var pulse = document.createElement( 'div' );
  pulse.className = 'pulse';
  holder.appendChild( pulse );

  // append the marker to the map container
  chart.svgContainer.htmlElement.appendChild( holder );

  $(holder).tooltip({});

  return holder;
}
</script>

我希望页面加载时显示圆圈,而不仅仅是 map 。非常感谢任何帮助。

最佳答案

I can zoom in and the circles show up.

这是一个重要的提示。此外,由于您的网站和 CMS 都存在同样的问题,因此 CMS 不是问题所在。

现在你只有:

chart.events.on( "mappositionchanged", updateCustomMarkers );

map 准备就绪后,您需要运行updateCustomMarkers,虽然您已经复制了our demo。 ,你错过了这条关键线:

chart.events.on( "ready", updateCustomMarkers );

有了它,无论是在您的网站上还是在 CMS 上,它都应该每次都能正常工作。

关于javascript - amCharts map 在页面刷新时未加载圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57678276/

相关文章:

javascript - 正则表达式只允许数字和小数在 Javascript 中不起作用

javascript - 我有悬停效果的问题

javascript - JQuery 文档 + 每个 + on

javascript - 在javascript中删除小数

javascript - Express.js CORS配置,PUT不起作用?

html - 使用 HTML5、CSS3 创建幻灯片的好工具

html - Bootstrap4 将登录表单移至中心

json - Jmeter:重用和更改 Json 响应作为下一个请求的正文

java - 根据位置过滤 Shodan 查询结果

json - 使用 SwiftyJSON 解析 Alamofire JSON 结果