:::success
1、加载中地地图文档
2、调用中地的地图服务查询点要素
3、如果查询到点要素就显示popup弹窗
:::
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../lib/include-openlayers-local.js"></script>
<script src="../../js/Tian.js"></script>
<!-- 调用中地服务查询 -->
<!-- <script src="../../js/query.js"></script> -->
<link rel="stylesheet" href="../../css/map.css">
</head>
<body>
<div id="map_container">
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content">
<h2>武汉</h2>
<img src="../../image/location.png" />
</div>
</div>
</div>
<script>
const docLayer = new Zondy.Map.Doc('', 'smart_city', {
ip: 'localhost',
port: 6163
})
const map = new ol.Map({
target: "map_container",
layers: [TianDiMap_vec, TianDiMap_cva, docLayer],
view: new ol.View({
center: [114, 30],
projection: 'EPSG:4326',
zoom: 4
})
})
/* 将DOM转化为overlay */
const popup = new ol.Overlay({
//要转换成overlay的HTML元素
element: document.querySelector('#popup'),
//当前窗口可见
autoPan: true,
//Popup放置的位置
positioning: 'bottom-center',
//是否应该停止事件传播到地图窗口
stopEvent: true,
autoPanAnimation: {
//当Popup超出地图边界时,为了Popup全部可见,地图移动的速度
duration: 250,
},
})
map.addOverlay(popup)
map.on('click', function (e) {
var coordinate = e.coordinate;
queryByPnt(coordinate)
})
document.querySelector('.ol-popup-closer').onclick = function () {
//设置位置为undefined
popup.setPosition(undefined)
}
function queryByPnt(point) {
//初始化查询结构对象,设置查询结构包含几何信息
var queryStruct = new Zondy.Service.QueryFeatureStruct();
queryStruct.IncludeGeometry = true;
//创建一个用于查询的点形状
var pointObj = new Zondy.Object.Point2D(point[0], point[1]);
//设置点容差半径
pointObj.nearDis = 1;
//实例化查询参数对象
var rule = new Zondy.Service.QueryFeatureRule({
//是否将要素的可见性计算在内
EnableDisplayCondition: false,
//是否完全包含
MustInside: false,
//是否仅比较要素的外包矩形
CompareRectOnly: false,
//是否相交
Intersect: true
})
var queryParam = new Zondy.Service.QueryParameter({
geometry: pointObj,
resultFormat: "json",
struct: queryStruct,
rule: rule
});
//实例化地图文档查询服务对象
var queryService = new Zondy.Service.QueryDocFeature(queryParam, "smart_city", 0, {
ip: "localhost",
port: "6163" //访问IGServer的端口号,.net版为6163,Java版为8089
});
//执行查询操作,querySuccess为查询回调函数
queryService.query(querySuccess, queryError);
}
//查询失败回调
function queryError(e) {
//停止进度条
stopPressBar();
}
//查询成功回调
function querySuccess(result) {
console.log(result)
//初始化Zondy.Format.PolygonJSON类
var format = new Zondy.Format.PolygonJSON()
//将MapGIS要素JSON反序列化 ol.Feature类型数组
var features = format.read(result)
console.log(features)
if (features) {
console.log(features[0])
let coordinate = features[0].getGeometry().flatCoordinates;
popup.setPosition(coordinate)
}
}
</script>
</body>
</html>