用openlayer是交互画一个点、线、面,执行回调函数
graphicLayer 是一个vector图层。
callback是回调函数。
调用方法如下:
if (typeof newlayer != ‘undefined’ && newlayer != null) {
var dg = new DiliGISApi.DrawGraphic(newlayer.Layer);
var calback = function(line) {
var pointCount = line.Value.length;
for (var i = 0; i <= pointCount – 1; i++) {
alert(line.Value[i][0] + “,” + line.Value[i][1]);
}
}
dg.DrawPolygon(calback);
} else {
alert(“create layer first”);
}
画图类如下:
var DiliGISApi = DiliGISApi ? DiliGISApi : {};
DiliGISApi.DrawGraphic = function(graphicLayer) {
///<summary>用于交互画地图对象/summary>
var _this = this;
var layer = graphicLayer;
/*
* 返回点对象
*/
_this.DrawPoint = function(callback) {
///<summary>画点</summary>
try {
var drawPointCtrol = new OpenLayers.Control.DrawFeature(layer, OpenLayers.Handler.Point);
drawPointCtrol.featureAdded = function(feature) {
try {
feature.layer.eraseFeatures([feature]);
var point = new DiliGISApi.Point(feature.geometry.x, feature.geometry.y);
callback(point);
drawPointCtrol.deactivate();
layer.map.removeControl(drawPointCtrol);
} catch (e) {
alert(“drawPointCtrol->:” + e.name + “,” + e.message);
}
}
layer.map.addControl(drawPointCtrol);
drawPointCtrol.activate();
} catch (e) {
alert(“DrawPoint->:” + e.name + “,” + e.message);
}
};
/*
* 返回线对象
*/
_this.DrawLine = function(callback) {
try {
var drawLineCtrol = new OpenLayers.Control.DrawFeature(layer, OpenLayers.Handler.Path);
drawLineCtrol.featureAdded = function(feature) {
try {
feature.layer.eraseFeatures([feature]);
var pts = feature.geometry.components;
var points = [];
var pointCount = pts.length;
for (var i = 0; i <= pointCount – 1; i++) {
points.push([pts[i].x, pts[i].y]);
}
var polyline = new DiliGISApi.PolyLine(points);
callback(polyline);
drawLineCtrol.deactivate();
layer.map.removeControl(drawLineCtrol);
} catch (e) {
alert(“drawLineCtrol->:” + e.name + “,” + e.message);
}
}
layer.map.addControl(drawLineCtrol);
drawLineCtrol.activate();
} catch (e) {
alert(“DrawLine->:” + e.name + “,” + e.message);
}
};
/*
* 返回面对象
*/
_this.DrawPolygon = function(callback) {
try {
var drawPolygonCtrol = new OpenLayers.Control.DrawFeature(layer, OpenLayers.Handler.Polygon);
drawPolygonCtrol.featureAdded = function(feature) {
try {
feature.layer.eraseFeatures([feature]);
var pts = feature.geometry.components[0].components;
var points = [];
var pointCount = pts.length;
for (var i = 0; i <= pointCount – 1; i++) {
points.push([pts[i].x, pts[i].y]);
}
var polygon = new DiliGISApi.Polygon(points);
callback(polygon);
drawPolygonCtrol.deactivate();
layer.map.removeControl(drawPolygonCtrol);
} catch (e) {
alert(“drawPolygonCtrol->:” + e.name + “,” + e.message);
}
}
layer.map.addControl(drawPolygonCtrol);
drawPolygonCtrol.activate();
} catch (e) {
alert(“DrawPolygon->:” + e.name + “,” + e.message);
}
};
};
转载自:https://blog.csdn.net/xuhaiyan8825/article/details/7201151