便利贴--6{OpenLayers,vue项目,画点}
同画线一样 ,简单粗暴,直接复用
只是加个数据在Draw.js中
_this.isPoint = options.isPoint;
/***
* 是否是点
*/
_this.coordinateOverPoint = options.coordinateOverPoint;
/***
* 输出点
*/
-------------------找到这个函数 加下面几行就好
Draw.prototype.handleUpEvent = function (event) {
// console.log(event.coordinate_, 'handleUpEvent')//获取坐标点
if (this.isPoint) {//判断是点直接抛出!
this.coordinateOverPoint(event);
this.finishDrawing();
return;
}
var pass = true;
if (this.getPointerCount() === 0) {
if (this.downTimeout_) {
clearTimeout(this.downTimeout_);
this.downTimeout_ = undefined;
}
this.handlePointerMove_(event);
if (this.shouldHandle_) {
var startingToDraw = !this.finishCoordinate_;
if (startingToDraw) {
//开始后抬起
// this.coordinateOverPoint(event);
this.coordinate(event);
this.startDrawing_(event.coordinate);
}
if (!startingToDraw && this.freehand_) {
this.finishDrawing();
} else if (
!this.freehand_ &&
(!startingToDraw || this.mode_ === Mode.POINT)
) {
//选择后抬起
this.coordinate(event);
if (this.atFinish_(event.pixel)) {
//结束绘画
this.coordinateOver("结束");
if (this.finishCondition_(event)) {
this.finishDrawing();
}
} else {
this.addToDrawing_(event.coordinate);
}
}
pass = false;
} else if (this.freehand_) {
this.abortDrawing();
}
}
if (!pass && this.stopClick_) {
event.preventDefault();
}
return pass;
};
主体函数--------注释没删 看情况使用,,,关于图片的尖头不在鼠标点的位置,–直接把图片用ps拉长 再到地图上调整偏移就好了···无奈之举若有更好的方法请指教(anchorYUnits: “-10px”,这个东西只能下移··)(offset: [0, 5],这个东西移动后超出图片范围会隐藏部分图片…)
getTypeSelectedPoint() {
this.tipTitle = "单击左键选择位置";
let that = this;
let isHaveOne = false;
$("#map")
.off("mousemove")
.mousemove(function (e) {
if (!that.showTip) {
that.showTip = true;
}
that.setTipPosition(e.offsetX, e.offsetY, 10, 10);
// console.log(e.clientX);
// console.log(e.offsetX);
// console.log(e.pageX);
// console.log(e.screenX);
// console.log("================================");
});
if (this.typeSelected !== "None") {
this.draw = new Draw({
source: this.drawLayer.getSource(),
type: this.typeSelected,
style: new Style({
stroke: new Stroke({
color: "red",
width: 3,
}),
}),
isPoint: true,
coordinate: function (res) {},
coordinateOverPoint: function (res) {
if (isHaveOne) {
return;
}
that.$store.commit("setPointData", res.coordinate); //用vuex传最终数据
// console.log(res.coordinate);//输出点
that.addIconMarker(res.coordinate);
that.butTitlePoint = "重新标点";
that.draw.controlDrawing(true);
$("#map").unbind("mousemove");
that.tipTitle = "";
that.showTip = false;
isHaveOne = true;
},
});
this.map.addInteraction(this.draw);
}
// 选点
// this.addIconMarker([115.9111272006128, 28.699307185841562]);
},
addIconMarker(intlat) {
this.carAddlayer.getSource().clear();
// if (this.olLayer) {
// window.ol2d.removeLayer(this.olLayer);
// }
// let iconFeatures;
// this.olLayer = new VectorLayer({
// // 图标图层
// zIndex: 22,
// source: new VectorSource(),
// });
let iconFeature = new OlFeature({
geometry: new OlGeomPoint(intlat), //绘制图形(点)
});
iconFeature.setStyle(
new OlStyleStyle({
image: new OlStyleIcon({
scale: 0.5,
//控制标注图片和文字之间的距离
// anchor: [0.2, 1],
//标注样式的起点位置
// anchorOrigin: "top-right",
//X方向单位:分数
// anchorXUnits: "10px",
//Y方向单位:像素
// anchorYUnits: "-10px",
//偏移起点位置的方向
offset: [0, 5],
offsetOrigin: "bottom",
//透明度
opacity: 1,
//图片路径
// src: require("img/logos.png"),
src: "img/dingwei/dingwei5.png",
}),
})
);
// 加载多个点用addFeatures,一个点用addFeature
// this.olLayer.getSource().addFeature(iconFeature);
this.carAddlayer.getSource().addFeature(iconFeature);
// window.ol2d.addLayer(this.olLayer);
// var startMarker = new OlFeature({
// type: "icon",
// // geometry: new OlGeomPoint([118.10131072998047, 36.819305419921875]),
// geometry: new OlGeomPoint(intlat),
// });
// var vectorLayer = new OlLayerVector({
// source: new OlSourceVector({
// features: [startMarker],
// }),
// style: new OlStyleStyle({
// image: new OlStyleIcon({
// anchor: [0.5, 1],
// src: "img/logos.png",
// // src: http://192.168.4.61:9527/static/mapMoniter/0.png
// // 说明下,因为vue项目打包之后,我这张图片放到了static目录下,直接给相对路径是无法访问到,
// // 可以通过js原生的API拿到对应的前台服务器ip和端口,还有项目名称,就可以访问到,下面附了对应帖子链接
// }),
// // 设置图片下面显示字体的样式和内容
// text: new Text({
// text: "文字描述", // 添加文字描述
// font: "14px font-size", // 设置字体大小
// // fill: new Fill({
// // // 设置字体颜色
// // color: "#1CAF9A",
// // }),
// offsetY: 10, // 设置文字偏移量
// }),
// }),
// });
// window.ol2d.addLayer(vectorLayer);
},