leaflet计算多边形⾯积
上⼀篇介绍了使⽤leaflet绘制圆形,那如何计算圆形的⾯积呢?
1、使⽤数学公式计算,绘制好圆形后,获取中⼼点以及半径即可
2、使⽤第三⽅⼯具计算,如.这⾥turf的area⽅法⼊参为geojson对象
3、使⽤超图的地图服务做⾯积计算,见下⽂范例
varmap,url=":8090/irver/rvices/map-china400/rest/maps/China";
map=('map',{
center:[33,114],
zoom:4
});
apLayer(url).addTo(map);
vareditableLayers=eGroup();
er(editableLayers);
vardrawControl=({
position:'topleft',
draw:{
polyline:fal,
polygon:fal,
circle:{},
rectangle:fal,
marker:fal,
remove:true
},
edit:{
featureGroup:editableLayers,
remove:true
}
});
trol(drawControl);
handleMapEvent(drawControl._container,map);
(D,function(e){
vartype=ype,
layer=;
if(type==='marker'){
pup('Apopup!');
}
er(layer);
//绘制完成后,获取圆形的半径和中⼼点坐标
varradius=layer._mRadius;
varlonlat=layer._latlng;
(radius+";"+lonlat);
//点集
//点集
varparts=[];
//计算圆的边缘所有点
for(vari=0;i<360;i++){
varradians=(i+1)*/180;
varcirclePoint=[(radians)*radius+,(radians)*radius+lonlat
.lng
];
parts[i]=circlePoint;
}
//⽤点集来构造leaflet的⾯对象
varpolygon1=n(parts);
//服务端计算⾯积
//getAreaByServer(polygon1);
//数学公式计算⾯积
//getAreaByClient(radius);
//turf计算⾯积
getAreaByTurf(SON());
});
functiongetAreaByClient(radius){
//使⽤数学公式或者⽤turf计算
varsquare=radius*radius*;
alert("⾯积:"+square+"平⽅⽶");
}
functiongetAreaByTurf(polygon){
vararea=(polygon);
alert("⾯积:"+area+"平⽅⽶");
}
functiongetAreaByServer(polygon){
//参数说明:eParameters(geometry,options)
//distanceMode(指定量算的⽅式为按球⾯长度'Geodesic'或者平⾯长度'Planar'来计算,默认是球⾯)
//prjCoordSys(指定该量算操作所使⽤的投影)
//unit(量算单位。默认量算结果以⽶为单位)
varareaMeasureParam=eParameters(polygon,{distanceMode:'Geodesic'});
ap
.measureService(url)
.measureArea(areaMeasureParam,function(rviceResult){
alert("⾯积:"++"平⽅⽶");
});
}
functionhandleMapEvent(div,map){
if(!div||!map){
return;
}
ntListener('mouover',function(){
e();
e();
});
ntListener('mouout',function(){
();
();
});
}