【问题标题】:How to plot clients geolocation on a Amcharts map (and deal with the asynchronous provision of the HTML5 geolocation)如何在 Amcharts 地图上绘制客户地理位置(并处理 HTML5 地理位置的异步提供)
【发布时间】:2019-06-06 10:24:15
【问题描述】:

我的 Web 应用程序中有一张世界地图,由 Amcharts4 提供支持。我想在地图上添加一个标记,显示用户的地理位置(使用 HTML5 getCurrentPosition 函数。但是在检索坐标时地图已经生成,如何在绘制后将新标记推送到地图?

我有 Amcharts 地图(包括地图上的标记)和地理定位功能的工作版本。 地理位置是异步工作的,脚本在检索位置时继续,因此同时生成地图。 我不喜欢等待找到坐标后再继续,因为这可能意味着延迟数秒。

本质上,我正在寻找在显示该对象后将标记数据推送到我的 Amcharts 对象的功能。 这是我的地图 js:

// Themes begin
am4core.useTheme(am4themes_animated);

// 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();

// Series for World map
var worldSeries = chart.series.push(new am4maps.MapPolygonSeries());
worldSeries.exclude = ["AQ"];
worldSeries.useGeodata = true;

worldSeries.data = mapdata;

var polygonTemplate = worldSeries.mapPolygons.template;
polygonTemplate.tooltipText = "{text}";
polygonTemplate.adapter.add("tooltipText", function(text, ev) {
  if (!ev.dataItem.dataContext.text) {
    return "{name}";
  }
  return text;
})

polygonTemplate.fill = chart.colors.getIndex(0);
polygonTemplate.nonScalingStroke = true;
polygonTemplate.propertyFields.fill = "fill";
polygonTemplate.properties.fill = am4core.color("#dddddd");

chart.zoomControl = new am4maps.ZoomControl();

// Hover state
var hs = polygonTemplate.states.create("hover");
hs.properties.fill = am4core.color("#333");

// Create image series
var imageSeries = chart.series.push(new am4maps.MapImageSeries());

// Create a circle image in image series template so it gets replicated to all new images
var imageSeriesTemplate = imageSeries.mapImages.template;
var circle = imageSeriesTemplate.createChild(am4core.Circle);
circle.radius = 4;
circle.fill = am4core.color("#000");
circle.stroke = am4core.color("#FFFFFF");
circle.strokeWidth = 2;
circle.nonScaling = true;
circle.tooltipText = "{title}";

// Set property fields
imageSeriesTemplate.propertyFields.latitude = "latitude";
imageSeriesTemplate.propertyFields.longitude = "longitude";

// Add data for the three cities
imageSeries.data = [{
  "latitude": 51.561678, 
  "longitude": 5.049685,
  "title": "Rural Spark HQ"
}];

if(navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } 
function showPosition(position) {
    clientlocation = ({"latitude": position.coords.latitude, "longitude": position.coords.longitude, "title": "I am here!"});
    console.log(clientlocation)
    //push this clientlocation data to the map somehow?
}

我可以在控制台中看到客户端位置,但是我没有找到任何方法可以在地图上获取它。

【问题讨论】:

    标签: javascript geolocation amcharts ammap


    【解决方案1】:

    您只需将新对象(clientlocation)添加到imageSeries.data

    imageSeries.addData(clientlocation);
    

    或者您可以通过重新分配数据字段来添加数据:

    imageSeries.data = [...imageSeries.data, clientlocation];
    

    Here 是作为参考的代码笔。

    【讨论】:

    • 我尝试了 imageSeries.data 方法,但没有让它工作。 addData 调用完成了这项工作。谢谢!
    猜你喜欢
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多