Skip to content

Commit 82051e1

Browse files
author
caoxinke
committed
添加OL3下测量的例子。
1 parent abfec1d commit 82051e1

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

build/deps.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ var deps = {
144144
"./src/OL3/SuperMap/iServer/TiledMapLayer.js",
145145
"./src/OL3/SuperMap/iServer/MapService.js",
146146
"./src/OL3/SuperMap/iServer/QueryService.js",
147-
"./src/OL3/SuperMap/iServer/GetLayersInfoService.js"
147+
"./src/OL3/SuperMap/iServer/GetLayersInfoService.js",
148+
"./src/OL3/SuperMap/iServer/MeasureService.js"
148149
]
149150
},
150151
"Data": {

examples/ol3/01_measure.html

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>距离测量</title>
6+
<link rel="stylesheet" href="../css/bootstrap.min.css">
7+
<link rel="stylesheet" href="../../dist/SuperMapiClient9 for OL3.css">
8+
<script type="text/javascript" src="../../dist/SuperMapiClient9 for OL3.js"></script>
9+
10+
<script type="text/javascript">
11+
var map, interaction, vectorLayer,
12+
url = "http://support.supermap.com.cn:8090/iserver/services/map-world/rest/maps/World";
13+
14+
function init() {
15+
map = new ol.Map({
16+
target: 'map',
17+
view: new ol.View({
18+
center: [0, 0],
19+
zoom: 3,
20+
projection: 'EPSG:4326'
21+
})
22+
});
23+
map.addLayer(new ol.supermap.TiledMapLayer(url, {"pro": "4326"}));
24+
}
25+
26+
function measureService(type) {
27+
clearLayer();
28+
var source = new ol.source.Vector({wrapX: false});
29+
vectorLayer = new ol.layer.Vector({
30+
source: source
31+
});
32+
map.addLayer(vectorLayer);
33+
if (type === 'distance') {
34+
interaction = new ol.interaction.Draw({
35+
source: source,
36+
type: "LineString",
37+
finishCondition: function (MapBrowserPointerEvent) {
38+
if (MapBrowserPointerEvent) {
39+
var measureService = new ol.supermap.MeasureService(url, {measureMode: ""});
40+
measureService.on("complete", function (serviceResult) {
41+
alert(serviceResult.element.result.distance);
42+
});
43+
var line = new ol.geom.LineString();
44+
var points = MapBrowserPointerEvent.frameState.pixelToCoordinateTransform;
45+
for (var i = 0; i < points.length; i = i + 2) {
46+
line.appendCoordinate([points[i], points[i + 1]]);
47+
}
48+
measureService.measureDistance(line);
49+
}
50+
return true;
51+
}
52+
})
53+
}
54+
if (type === 'area') {
55+
interaction = new ol.interaction.Draw({
56+
source: source,
57+
type: "Polygon",
58+
finishCondition: function (MapBrowserPointerEvent) {
59+
if (MapBrowserPointerEvent) {
60+
var measureService = new ol.supermap.MeasureService(url, {measureMode: ""});
61+
measureService.on("complete", function (serviceResult) {
62+
alert(serviceResult.element.result.area);
63+
});
64+
var points = MapBrowserPointerEvent.frameState.pixelToCoordinateTransform;
65+
var polygonCoords = [];
66+
for (var i = 0; i < points.length; i = i + 2) {
67+
polygonCoords.push([points[i], points[i + 1]]);
68+
}
69+
measureService.measureArea(new ol.geom.Polygon([polygonCoords]));
70+
}
71+
return true;
72+
}
73+
})
74+
}
75+
map.addInteraction(interaction);
76+
}
77+
78+
function clearLayer() {
79+
map.removeInteraction(interaction);
80+
if (vectorLayer) {
81+
map.removeLayer(vectorLayer);
82+
}
83+
}
84+
85+
</script>
86+
</head>
87+
<body onload="init()" style=" margin: 0;overflow: hidden;background: #fff;">
88+
<div id="toolbar" style=" position: relative;padding-top: 10px; padding-bottom: 10px;">
89+
<input type="button" class="btn btn-primary" value="测距离" onclick="measureService('distance')"/>
90+
<input type="button" class="btn btn-primary" value="测面积" onclick="measureService('area')"/>
91+
<input type="button" class="btn btn-primary" value="清除" onclick="clearLayer()"/>
92+
</div>
93+
<div id="map" style="margin:0 auto;position: relative; height: 510px;border: 1px solid #3473b7;"></div>
94+
</body>
95+
</html>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Class:MeasureService
3+
* 距离测量服务
4+
*/
5+
require('./ServiceBase');
6+
require('../../../Core/iServer/MeasureService');
7+
8+
ol.supermap.MeasureService = function (url, options) {
9+
ol.supermap.ServiceBase.call(this, url, options);
10+
}
11+
ol.inherits(ol.supermap.MeasureService, ol.supermap.ServiceBase);
12+
13+
ol.supermap.MeasureService.prototype.measureDistance = function (measureGeo) {
14+
this.measure(measureGeo, 'DISTANCE');
15+
};
16+
17+
ol.supermap.MeasureService.prototype.measureArea = function (measureGeo) {
18+
this.measure(measureGeo, 'AREA');
19+
};
20+
21+
ol.supermap.MeasureService.prototype.measure = function (measureGeo, type) {
22+
var me = this, geometry = me._processGeometry(measureGeo);
23+
var measureService = new SuperMap.REST.MeasureService(me.options.url, {
24+
measureMode: type,
25+
eventListeners: {
26+
scope: me,
27+
processCompleted: me.processCompleted,
28+
processFailed: me.processFailed
29+
}
30+
});
31+
measureService.processAsync(new SuperMap.REST.MeasureParameters(geometry));
32+
return me;
33+
};
34+
35+
ol.supermap.MeasureService.prototype._processGeometry = function (measureGeo) {
36+
if (measureGeo) {
37+
measureGeo = ol.supermap.Util.toSuperMapGeometry(JSON.parse((new ol.format.GeoJSON()).writeGeometry(measureGeo)));
38+
}
39+
return measureGeo;
40+
};
41+
module.exports = ol.supermap.MeasureService;

0 commit comments

Comments
 (0)