地图工具
start(line:any,point:any,obj?:any) {
line.animateShow({
duration: obj.time*1000 || 10000,
easing: "linear"
},
function(_frame:any, c:any) {
currentCoordinate = c.copy();
point.setCoordinates(currentCoordinate);
if (preCoordinate) {
if (!window.map.isInteracting()) {
window.map.setCenter(currentCoordinate);
if(obj.bearing){
const bearing = MapTool.getBearing(preCoordinate, currentCoordinate);
window.map.setBearing(bearing);
}
}
}
preCoordinate = c.copy();
});
},
getBearing(c1:any, c2:any) {
const p1 = turf.point(c1.toArray()),
p2 = turf.point(c2.toArray());
const bearing = turf.bearing(p1, p2);
if (Math.abs(window.map.getBearing() - bearing) < 30) {
return window.map.getBearing();
}
return bearing;
},
drawJujiCircle(c:any,color?:any){
let val:any;
val = colorRevert(color);
let colorNew = JSON.parse(val);
return new ispace.Circle(c,15, {
symbol: {
lineColor: 'rgba(208, 194, 116,0)',
polygonFill: {
opacity: 0.5,
type: "radial",
colorStops: colorNew,
},
polygonOpacity: 1,
lineWidth: 0,
},
})
},
jujiCircleMove(data:any,c:any,layer:any,guijiTime:any,speed:number=1,color?:any){
let arr=<any[]>[];
for(let item of data){
arr.push([item.lon,item.lat]);
}
const rect3 = MapTool.drawJujiCircle(c,color).addTo(layer);
let line = MapTool.drawLine(arr,"rgba(208, 194, 116,0.2)").addTo(layer);
MapTool.start(line,rect3,{time:guijiTime*speed,bearing:false});
},
chaoyuan(geoJson:any,color:any,layer:any,pointsNum:number,imgUrl:string){
let geometry = geoJson&&ispace.Geometry.fromJSON(geoJson);
if(!geometry)return;
geometry.setSymbol({
polygonFill:color,
polygonOpacity:0.6,
lineColor:color
})
geometry.addTo(layer);
let extent = layer.getExtent();
for(let i=0;i<pointsNum;i++){
var position = turf.randomPosition([extent.xmin,extent.ymin,extent.xmax,extent.ymax])
MapTool.drawPoint(position,imgUrl).addTo(layer);
}
window.map.animateTo({
zoom : 18,
center :extent.getCenter(),
}, {
duration : 1000,
easing : 'out'
});
},
getRandomCoordinate(centerX:number, centerY:number, radius:number) {
var angle = Math.random() * 2 * Math.PI;
var distance = (Math.random() * radius)/111300;
var x = centerX + distance * Math.cos(angle);
var y = centerY + distance * Math.sin(angle);
return { x: x, y: y };
},
转化颜色(RGB颜色值与十六进制颜色码互相转换)
export function colorRevert(color:any) {
if(color.indexOf('rgb')>-1){
return getResColor(color);
}else{
let val = hexToRgb(color);
return getResColor(val.rgb);
}
}
function getResColor(color:any){
if(color.indexOf('rgba')>-1){
let val = color.split(",");
return `[[0, "${val[0]},${val[1]},${val[2]},0)"],
[0.2, "${val[0]},${val[1]},${val[2]},0.2)"],
[0.6, "${val[0]},${val[1]},${val[2]},0.6)"],
[1.0, "${val[0]},${val[1]},${val[2]},1)"]]`
}else if(color.indexOf('rgb')> -1){
let val = color.split(",");
let transitionValBefore = val[0].slice(0,3)+'a'+val[0].slice(3);
let iconIndex = val[2].indexOf(")");
return `[[0, "${transitionValBefore},${val[1]},${val[2].slice(0,iconIndex)},0${val[2].slice(-1)}"],
[0.2, "${transitionValBefore},${val[1]},${val[2].slice(0,iconIndex)},0.2${val[2].slice(-1)}"],
[0.6, "${transitionValBefore},${val[1]},${val[2].slice(0,iconIndex)},0.6${val[2].slice(-1)}"],
[1.0, "${transitionValBefore},${val[1]},${val[2].slice(0,iconIndex)},1${val[2].slice(-1)}"]]`
}
}
export function hexToRgb(val:any) {
var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
var color = val.toLowerCase();
var result = '';
if (reg.test(color)) {
if (color.length === 4) {
var colorNew = "#";
for (var i = 1; i < 4; i += 1) {
colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
}
color = colorNew;
}
var colorChange = [];
for (var i = 1; i < 7; i += 2) {
colorChange.push(parseInt("0x" + color.slice(i, i + 2)));
}
result = "rgb(" + colorChange.join(",") + ")";
return { rgb: result, r: colorChange[0], g: colorChange[1], b: colorChange[2] };
} else {
result = '无效';
return { rgb: result };
}
}