/*
|
*cesium组件封装
|
*20190307
|
*/
|
export default{
|
install(Vue){
|
/*
|
*导航属性
|
*/
|
Vue.prototype.CesiumAPI = {
|
/*
|
*导航接口
|
*/
|
NavigateObj: {
|
/*
|
*飞到当前位置
|
*参数x:经度
|
*参数y:纬度
|
*参数h:高度
|
*/
|
flyToXYZ: (Cesium, Viewerc, x, y, z) => {
|
Viewerc.camera.flyTo({
|
destination: Cesium.Cartesian3.fromDegrees(x, y, z)
|
});
|
},
|
/*
|
*放大 视角拉近
|
*/
|
zoomIn: (Viewerc) => {
|
Viewerc.camera.zoomIn((1000000))
|
},
|
/*
|
*缩小 视角拉远
|
*/
|
zoomOut: (Viewerc) => {
|
Viewerc.camera.zoomOut((1000000))
|
},
|
/*
|
*跳转
|
* 参数:Viewerc,Cesium,obj
|
*/
|
JumpTo: (Viewerc, Cesium, obj) => {
|
if (obj.position != null && obj.position != undefined) {
|
Viewerc.camera.setView({
|
destination: obj.position,
|
orientation: {
|
heading: Cesium.Math.toRadians(0),
|
pitch: Cesium.Math.toRadians(-45.0),
|
roll: 0.0
|
}
|
});
|
}
|
}
|
},
|
/*
|
*创建接口
|
*/
|
CreateObj: {
|
/*
|
*创建点
|
*参数point:必选项 点的属性 {
|
position: cartesian,
|
point: {
|
color: Cesium.Color.RED,
|
pixelSize: 5,
|
heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
|
}
|
};
|
*/
|
createPoint: (tag, Viewerc, point, callback) => {
|
var temp = _isContain(createobjlist, tag);
|
if (temp.index > -1) {
|
Viewerc.entities.remove(temp.item);
|
createobjlist.splice(temp.index, 1);
|
} else {
|
var en = Viewerc.entities.add(point);
|
createobjlist.push({tag: tag, item: en});
|
if (typeof callback == 'function') {
|
callback({tag: tag, item: en, point: point});
|
}
|
}
|
},
|
/*
|
*功能:创建线 左键开始 右键结束 创建
|
*参数
|
*/
|
createLine: (tag, Viewerc, Cesium, handler, callback) => {
|
var createLine;
|
var temp = _isContain(createobjlist, tag);
|
if (temp.index > -1) {
|
Viewerc.entities.remove(temp.item);
|
createobjlist.splice(temp.index, 1);
|
} else {
|
|
var PolyLinePrimitive = (function () {
|
function _(positions) {
|
this.options = {
|
polyline: {
|
show: true,
|
positions: [],
|
material: Cesium.Color.RED,
|
width: 3
|
}
|
};
|
this.positions = positions;
|
this._init();
|
}
|
|
_.prototype._init = function () {
|
var _self = this;
|
var _update = function () {
|
return _self.positions;
|
};
|
//实时更新polyline.positions
|
this.options.polyline.positions = new Cesium.CallbackProperty(_update, false);
|
createLine = Viewerc.entities.add(this.options);
|
|
};
|
return _;
|
})();
|
var positions = [];
|
var poly = undefined;
|
//鼠标左键单击画点
|
handler.setInputAction(function (movement) {
|
var cartesian = Viewerc.scene.camera.pickEllipsoid(movement.position, Viewerc.scene.globe.ellipsoid);
|
if (positions.length == 0) {
|
positions.push(cartesian.clone());
|
}
|
positions.push(cartesian);
|
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
|
//鼠标移动
|
handler.setInputAction(function (movement) {
|
var cartesian = Viewerc.scene.camera.pickEllipsoid(movement.endPosition, Viewerc.scene.globe.ellipsoid);
|
if (positions.length >= 2) {
|
if (!Cesium.defined(poly)) {
|
poly = new PolyLinePrimitive(positions);
|
} else {
|
if (cartesian != undefined) {
|
positions.pop();
|
cartesian.y += (1 + Math.random());
|
positions.push(cartesian);
|
}
|
}
|
}
|
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
//单击鼠标右键结束画线
|
handler.setInputAction(function (movement) {
|
handler.destroy();
|
new PolyLinePrimitive(positions);
|
if (typeof callback == 'function') {
|
callback({tag: tag, item: createLine});
|
}
|
createobjlist.push({tag: tag, item: createLine});
|
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
|
}
|
|
},
|
/*
|
*功能:画面 左键开始 右键结束
|
*/
|
createPolygon: (tag, Viewerc, Cesium, handler, callback) => {
|
var temp = _isContain(createobjlist, tag);
|
if (temp.index > -1) {
|
Viewerc.entities.remove(temp.item);
|
createobjlist.splice(temp.index, 1);
|
} else {
|
var Polygon;
|
var PolygonPrimitive = (function () {
|
function _(positions) {
|
this.options = {
|
name: '多边形',
|
polygon: {
|
hierarchy: [],
|
perPositionHeight: true,
|
material: Cesium.Color.RED.withAlpha(0.4)
|
}
|
};
|
this.hierarchy = positions;
|
this._init();
|
}
|
|
_.prototype._init = function () {
|
var _self = this;
|
var _update = function () {
|
return _self.hierarchy;
|
};
|
//实时更新polygon.hierarchy
|
this.options.polygon.hierarchy = new Cesium.CallbackProperty(_update, false);
|
Polygon = Viewerc.entities.add(this.options);
|
};
|
return _;
|
})();
|
|
var positions = [];
|
var poly = undefined;
|
|
//鼠标单击画点
|
handler.setInputAction(function (movement) {
|
var cartesian = Viewerc.scene.camera.pickEllipsoid(movement.position, Viewerc.scene.globe.ellipsoid);
|
if (positions.length == 0) {
|
positions.push(cartesian.clone());
|
}
|
positions.push(cartesian);
|
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
|
//鼠标移动
|
handler.setInputAction(function (movement) {
|
var cartesian = Viewerc.scene.camera.pickEllipsoid(movement.endPosition, Viewerc.scene.globe.ellipsoid);
|
if (positions.length >= 2) {
|
if (!Cesium.defined(poly)) {
|
poly = new PolygonPrimitive(positions);
|
} else {
|
if (cartesian != undefined) {
|
positions.pop();
|
cartesian.y += (1 + Math.random());
|
positions.push(cartesian);
|
}
|
}
|
}
|
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
//鼠标右键单击结束绘制
|
handler.setInputAction(function (movement) {
|
handler.destroy();
|
new PolygonPrimitive(positions);
|
if (typeof callback == 'function') {
|
callback({tag: tag, item: Polygon});
|
}
|
createobjlist.push({tag: tag, item: Polygon});
|
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
|
}
|
},
|
/*画矩形 左键开始 右键结束*/
|
drawRect: (tag, _this,Viewerc, Cesium, handle, callback) => {
|
var temp = _isContain(createobjlist, tag);
|
if (temp.index > -1) {
|
Viewerc.entities.remove(temp.item);
|
createobjlist.splice(temp.index, 1);
|
} else {
|
var _self = _this;
|
var pointsArr = [];
|
_self.shape = {
|
points: [],
|
rect: null,
|
entity: null
|
};
|
var tempPosition;
|
/*var handle = new Cesium.ScreenSpaceEventHandler(_self.viewer.scene.canvas);*/
|
//鼠标左键单击画点
|
handle.setInputAction(function (click) {
|
tempPosition = _self.getPointFromWindowPoint(click.position);
|
//选择的点在球面上
|
if (tempPosition) {
|
if (_self.shape.points.length == 0) {
|
pointsArr.push(tempPosition);
|
_self.shape.points.push(_self.viewer.scene.globe.ellipsoid.cartesianToCartographic(tempPosition));
|
_self.shape.rect = Cesium.Rectangle.fromCartographicArray(_self.shape.points);
|
_self.shape.rect.east += 0.000001;
|
_self.shape.rect.north += 0.000001;
|
_self.shape.entity = _self.viewer.entities.add({
|
rectangle: {
|
coordinates: _self.shape.rect,
|
material: Cesium.Color.BLACK.withAlpha(0.4),
|
outline: true,
|
outlineWidth: 2,
|
outlineColor: Cesium.Color.RED,
|
height: 0
|
}
|
});
|
_self.bufferEntity = _self.shape.entity;
|
} else {
|
handle.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
handle.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
|
callback(pointsArr);
|
}
|
}
|
}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
|
//鼠标移动
|
handle.setInputAction(function (movement) {
|
if (_self.shape.points.length == 0) {
|
return;
|
}
|
var moveEndPosition = _self.getPointFromWindowPoint(movement.endPosition);
|
//选择的点在球面上
|
if (moveEndPosition) {
|
pointsArr[1] = moveEndPosition;
|
_self.shape.points[1] = _self.viewer.scene.globe.ellipsoid.cartesianToCartographic(moveEndPosition);
|
_self.shape.rect = Cesium.Rectangle.fromCartographicArray(_self.shape.points);
|
if (_self.shape.rect.west == _self.shape.rect.east)
|
_self.shape.rect.east += 0.000001;
|
if (_self.shape.rect.south == _self.shape.rect.north)
|
_self.shape.rect.north += 0.000001;
|
_self.shape.entity.rectangle.coordinates = _self.shape.rect;
|
}
|
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
|
}
|
}
|
}
|
}
|
}
|
}
|
/*
|
*判断当前数组是否存在该对象
|
*/
|
window._isContain = function(layerlist, tag) {
|
for (var i = 0; i < layerlist.length; i++) {
|
var temp = layerlist[i];
|
if (temp.tag == tag) {
|
temp.index = i;
|
return temp;
|
}
|
}
|
return { item: null, index: -1 };
|
}
|
window.createobjlist = [];
|