我的工作amcharts map
在angular
。我使用lat
和绑定位置long
,我没有国家/地区,id
而是有位置名称。我想使用缩放特定位置zoomToMapObject
。
我有一个下拉菜单,当我从其中选择一个国家时,我想对其进行缩放。
这是我object
的locationData
{
"dId": 28046,
"lat": 33.8627681,
"long": -117.8870831,
"loc": "Fullerton CA"
"color":"red"
},
{
"dId": 25535,
"lat": 31.5649775,
"long": -110.2591639,
"loc": "Sierra Vista AZ"
"color":"yellow"
},
"dId": 31222,
"lat": 33.4308114,
"long": -70.7828727,
"loc": "Pudahuel Metropolitana"
"color":"green"
},
{
"dId": 23280,
"lat": 36.1676717,
"long": -94.1298177,
"loc": "Springdale AR"
"color":"red"
},
当我选择下拉菜单时,SelectCountry
我将如何使我的代码工作。所有这些变量都在返回undefined
,因此zoomToMapObject
不起作用
public mapCountry_selected(country)
{
setTimeout( () => {
this.mapChart.zoomToGeoPoint({latitude:country.lat,longitude:country.long},17,true)
}, 100);
}
这是我的代码
public locationMap()
{
this.mapChart = am4core.create("productsMap", am4maps.MapChart);
this.mapChart .geodata = am4geodata_worldLow;
this.mapChart .projection = new am4maps.projections.Miller();
this.polygonSeries = this.mapChart .series.push(new am4maps.MapPolygonSeries());
this.polygonSeries.exclude = ["AQ"];
this.polygonSeries.useGeodata = true;
this.polygonSeries.calculateVisualCenter = true;
let imageSeries = this.mapChart .series.push(new am4maps.MapImageSeries());
var place = imageSeries.mapImages.template;
place.nonScaling = true;
place.propertyFields.latitude = "lat";
place.propertyFields.longitude = "long";
imageSeries.data=this.locationData;
var circle = place.createChild(am4core.Circle);
circle.propertyFields.fill = "color";
imageSeries.heatRules.push({
"target": circle,
"property": "radius",
"min": 3,
"max": 10,
"dataField": "value",
})
}
编辑1:
我正在"target": circle
用绿色,黄色,红色三种颜色显示每个location()的圆圈。现在在添加true
第二点和第三点之后(根据我的评论)。从下拉列表中选择一个后,我需要突出显示/弹出位置,因此用户可以直接知道我选择的这个位置,因为彼此之间有很多位置。
编辑2:
从下拉菜单中选择位置后的地图
根据可用的信息,使用MapChart对象上可用的zoomToGeoPoint方法可能更容易考虑,例如
//assuming the selected country is as follows
var country = {
"dId": 23280,
"lat": 36.1676717,
"long": -94.1298177,
"loc": "Springdale AR",
"color":"red",
};
this.mapChart.zoomToGeoPoint({latitude:country.lat,longitude:country.long},17)
17
您所需的缩放级别在哪里?zoomToMapObject
在处理图表事件时特别有用。
但是,如果您想使用zoomToMapObject方法,则根据您的示例,可以考虑使用以下方法。
对于数据集中的每个数据点,并使用每个多边形am4geodata_worldLow
具有“ id”(例如,提取第一个ID的ID am4geodata_worldLow.features[0].id
)的初始数据,您可以使用lat / lng坐标确定每个国家/地区最接近每个多边形的中心。
在MapChart
您存储的对象中,您必须this.mapChart
检查多边形或MapObjects的多个系列(通常被视为图层)。可以使用以下方法提取这些:
var mapPolygons = this.mapChart.series.values.flatMap(function(seriesItem){return seriesItem.mapPolygons.values;});
然后,您可以构建一个数据结构,将每个自定义国家/地区映射到地图中可用的ID(例如,对象{"your-country-id":mapPolygonObject}
)
对于每个,mapPolygon
您都可以使用
var coordinates = mapPolygon.points[0][0]; //returns an array of x,y coordinates
//eg
coordinates =
[{"x":691.2713756097037,"y":137.68935508639663},{"x":691.2544487420046,"y":137.69150482342081},{"x":691.2544487420046,"y":137.67239542245633},{"x":691.2695833531237,"y":137.6702455263596}];
然后,您可以使用这些点作为边界来找到该多边形的中心/质心,然后对于您的每个国家/地区点,您都将找到多边形中心点与您所在国家/地区之间的欧几里得距离。最短距离可以假定该国家/地区与此点相关。可能会考虑距离(注意。由于您使用的是坐标,因此请确保至少考虑5个小数点)为50,000英里(请注意您的坐标系,以将经纬度转换为米/英里)意味着这些是不同的地方。
然后,您可以提取ID并构建地图对象,以供稍后在脚本中使用。这可能会很耗时且需要大量计算,并且如果您之前进行过此操作并存储了必要的引用以供以后使用,则理想情况下会更好。
此外,如果您具有国家/地区ID,则搜索将更加简单,如下所示:
//assuming the selected country is as follows
var country = {
"dId": 23280,
"lat": 36.1676717,
"long": -94.1298177,
"loc": "Springdale AR",
"color":"red",
};
//for this example I am extracting the country id from the object,it would be ideal to have it otherwise
//NB. country ids are also available in the `am4geodata_worldLow` used to initialize the map
var countryId = country.loc.split(" ")[1]; //this would return the country code - 'AR'
//we will now search the created chart for mapObjects with this id
//this will give us an array of map objects/polygons with the id if found
var possibleMapObjects = this.mapChart.series.values.map(function(seriesItem){
return seriesItem.getPolygonById(countryId)
}).flat();
if(possibleMapObjects.length > 0){
// we have found a map object
var countryMapObject = possibleMapObjects[0]; //taking the first item of array
//now we can zoom to map object
//the 3rd parameter true implies centering the map
this.mapChart.zoomToMapObject(countryMapObject,17,true)
}
根据更新的代码,我注意到了一些问题,无法在您的地图上看到标记,因此我进行了一些修改。此外,由于我已使用您使用的ID扩充了数据,因此修改现在允许您按ID检索标记/地图对象,请参见下面的更新代码。
我已经创建了一个下拉/选择节点来模拟onchange事件,在回调/事件处理程序中,onCountryChange
您将看到如何通过我们指定的新伪ID检索标记并调用操作(我将打开一个弹出窗口),到已记录的MapImage API
locationData = [
{
"dId": 28046,
"lat": 33.8627681,
"long": -117.8870831,
"loc": "Fullerton CA",
"color": "red"
},
{
"dId": 25535,
"lat": 31.5649775,
"long": -110.2591639,
"loc": "Sierra Vista AZ",
"color": "yellow"
},
{
"dId": 31222,
"lat": 33.4308114,
"long": -70.7828727,
"loc": "Pudahuel Metropolitana",
"color": "green"
},
{
"dId": 23280,
"lat": 36.1676717,
"long": -94.1298177,
"loc": "Springdale AR",
"color": "red"
},
].map(function(place) {
place.id = place.dId;
return place;
});
this.mapChart = am4core.create("productsMap", am4maps.MapChart);
this.mapChart.geodata = am4geodata_worldLow;
this.mapChart.projection = new am4maps.projections.Miller();
this.polygonSeries = this.mapChart.series.push(new am4maps.MapPolygonSeries());
this.polygonSeries.exclude = ["AQ"];
this.polygonSeries.useGeodata = true;
this.polygonSeries.calculateVisualCenter = true;
let imageSeries = this.mapChart.series.push(new am4maps.MapImageSeries());
var place = imageSeries.mapImages.template;
place.nonScaling = true;
place.propertyFields.latitude = "lat";
place.propertyFields.longitude = "long";
imageSeries.data = locationData;
var circle = place.createChild(am4core.Circle);
circle.propertyFields.fill = "color";
/// code below made circles visible and used value in {loc} / location name as tooltiptext
circle.width = 20;
circle.height = 20;
circle.nonScaling = false;
circle.tooltipText = "{loc}";
circle.horizontalCenter = "middle";
circle.verticalCenter = "bottom";
var circle2 = imageSeries.mapImages.template.createChild(am4core.Circle);
circle2.radius = 3;
circle2.propertyFields.fill = "color";
circle2.events.on("inited", function(event){
animateBullet(event.target);
})
function animateBullet(circle) {
var animation = circle.animate([{ property: "scale", from: 1, to: 5 },
{property: "opacity", from: 1, to: 0 }], 1000, am4core.ease.circleOut);
animation.events.on("animationended", function(event){
animateBullet(event.target.object);
})
}
imageSeries.heatRules.push({
"target": circle,
"property": "radius",
"min": 3,
"max": 10,
"dataField": "value",
});
//just initializing the ui (you have already done this in angular)
var ddlPlaceChooser = document.getElementById("ddlPlaceChooser");
locationData.map(function(location) {
var option = document.createElement("option");
option.value = location.id;
option.innerText = location.loc;
return option;
})
.forEach(function(option) {
ddlPlaceChooser.appendChild(option)
})
function onCountryChange(event) {
//however you retrieve the selected id
var placeId = ddlPlaceChooser.selectedOptions[0].value;
//retrieve map object
var mapObject = imageSeries.getImageById(placeId);
//zoom to map object
mapChart.zoomToMapObject(mapObject, 17, true);
//optionally open popup with message of choice
mapObject.openModal("You choose me!")
//mapObject.openPopup("You choose me!")
}
ddlPlaceChooser.addEventListener('change', onCountryChange)
<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://cdn.amcharts.com/lib/4/themes/animated.js"></script>
<div>
<label for="placeChooser">Choose Place</label>
<select id="ddlPlaceChooser" name="placeChooser"></select>
</div>
<hr />
<div id="productsMap"></div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句