-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_search.html
232 lines (194 loc) · 6.74 KB
/
map_search.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<!DOCTYPE html>
<html>
<head lang="en">
<title>Quick Start</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0 maximum-scale=1.0 user-scalable=no">
<link rel="stylesheet" type="text/css" href="./NGR.css">
<script src="./NGR.min.js"></script>
</head>
<body style="height: 100%; margin: 0; padding: 0;">
<div id="map" style="width: 100%; height: 100%; margin: 0; padding: 0; position:absolute"></div>
<script>
(function () {
window.map = new NGR.View('map', {
appKey: "b7a7263084694c20b5294ed1e0aaf311"
});
window.res = new NGR.DataSource({
appKey: "b7a7263084694c20b5294ed1e0aaf311"
});
window.layers = {};
var mapId = 171;
var marker = null;
// 搜索中心经纬度
var centerLatLng = NGR.latLng([31.236859692993924, 121.4996239542961]);
// 搜索中心WGS84坐标
var coord = NGR.CRS.EPSG3857.project(centerLatLng);
var control = NGR.control.floor();
var onerror = function (e) {
console.error(e, e.stack);
};
// 请求楼层数据
var loadPlanarGraph = function (planarGraphId) {
res.requestPlanarGraph(planarGraphId).then(function (layerInfo) {
console.log(layerInfo);
NGR.IO.fetch({
url: './style.json',
onsuccess: JSON.parse
}).then(function (style) {
map.clear();
// 清除marker和查询页
marker = null;
removeSearchPad();
// 加载各个楼层
layers.frame = NGR.featureLayer(layerInfo, {
layerType: 'Frame',
styleConfig: style
});
layers.area = NGR.featureLayer(layerInfo, {
layerType: 'Area',
styleConfig: style
});
layers.annotation = NGR.featureLayer(layerInfo.Area, {
layerType: 'LogoLabel',
styleConfig: style
});
layers.facility = NGR.featureLayer(layerInfo.Facility, {
layerType: 'Facility',
styleConfig: style
});
layers.collision = NGR.layerGroup.collision({
margin: 3
});
layers.collision.addLayer(layers.annotation);
layers.collision.addLayer(layers.facility);
map.addLayer(layers.frame);
map.addLayer(layers.area);
map.addLayer(layers.collision);
// 绘制搜索范围
var circle = NGR.circle(centerLatLng, 30, { color: 'blue', weight: 1 });
map.addLayer(circle);
// 渲染地图
map.render();
}).fail(onerror);
}).fail(onerror);
};
// 合并高亮字段下标
var mergeSegments = function (segments, segment) {
var newSegments = [];
var newSegment = segment.slice()
for (var i = 0; i < segments.length; ++i) {
var tempSegment = segments[i];
if (newSegment[0] > tempSegment[1] || newSegment[1] < tempSegment[0]) {
newSegments.push(segments.slice());
} else {
newSegment[0] = Math.min(newSegment[0], tempSegment[0]);
newSegment[1] = Math.max(newSegment[1], tempSegment[1]);
}
}
newSegments.push(newSegment);
return newSegments;
};
// 处理高亮字段
var highlight = function (src, keywords) {
var offset = NGR.DataUtils.getOffset([src], keywords)[0];
var textArray = src.split('');
var mergeOffset = [];
var insertOffset = 0;
for (var i = 0; i < offset.length; i += 2) {
var segment = [offset[i], offset[i + 1]];
mergeOffset = mergeSegments(mergeOffset, segment);
}
mergeOffset.sort();
for (var i = 0; i < mergeOffset.length; ++i) {
textArray.splice(mergeOffset[i][0] + insertOffset++, 0, '<b>');
textArray.splice(mergeOffset[i][1] + insertOffset++, 0, '</b>');
}
return textArray.join('');
}
// 创建查询结果页
var createSearchPad = function (list, top, transform) {
var searchPad = NGR.DomUtil.create('div', 'search-pad ngr-bar', document.body);
searchPad.style.top = top;
searchPad.style.transform = transform;
for (var i in list) {
var poi = list[i];
var item = NGR.DomUtil.create('a', '', searchPad);
var text = poi.name;
item.innerHTML = highlight(text, poi.highlights);
item.href = '#';
item.setAttribute('poi', poi.id);
// 点击查询结果在地图上显示marker
item.addEventListener('click', function (e) {
var layer = layers.area.getLayerByFeatureId(e.target.getAttribute('poi'));
var latLng = layer.getCentroid();
if (!NGR.PolyUtil.PolygonContainsLatLng(layer, latLng)) {
latLng = layer.getInnerPoint();
}
if (marker) {
map.removeLayer(marker);
marker = null;
}
marker = NGR.marker(latLng, {
icon: NGR.icon({
iconUrl: '../../../resources/images/marker_icon@2x.png',
iconSize: [25, 32],
}),
});
map.addLayer(marker);
removeSearchPad();
});
}
};
var removeSearchPad = function () {
var pads = document.getElementsByClassName('search-pad');
for (var i = 0; i < pads.length; ++i) {
var pad = pads[i];
document.body.removeChild(pad);
}
};
// 初始化查询框
var input = NGR.DomUtil.create('input', 'platform-demo-input', document.body);
var searchImg = NGR.DomUtil.create('img', 'platform-demo-input-image', document.body);
var inputWidth = 400;
var imageSize = 26;
input.style.width = inputWidth + 'px';
input.style.transform = 'translate(' + (window.innerWidth / 2 - inputWidth / 2) + 'px)';
searchImg.src = '../../../resources/images/search.png';
searchImg.style.height = imageSize + 'px';
searchImg.style.width = imageSize + 'px';
searchImg.style.transform = 'translate(' + (window.innerWidth / 2 + inputWidth / 2 + 15) + 'px)';
searchImg.addEventListener('click', function (e) {
removeSearchPad();
var keywords = input.value;
// 搜索poi
res.POISearch({
keywords: keywords,
parents: [control.getCurrentFloor()],
coordinate: {x: coord.x, y: coord.y},
distance: 30,
start: 1,
count: 5,
}).then(function (response) {
if (response.total === 0) {
return ;
}
createSearchPad(response.list, '42px', input.style.transform);
}).fail(onerror);
});
// 请求地图数据
res.requestMap(mapId).then(function (mapInfo) {
res.requestPOIChildren(mapInfo.poi).then(function(floors) {
// 初始化楼层控件
map.addControl(control);
control.on('change', function (e) {
control.setCurrentFloor(e.to, loadPlanarGraph);
});
control.setFloorList(floors);
control.setCurrentFloor(floors[3].id, loadPlanarGraph);
}).fail(onerror);
}).fail(onerror);
}).call(this);
</script>
</body>
</html>