<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<title>获取某类覆盖物</title>
<link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
<style>
html, body, #container {
height: 100%;
width: 100%;
}
.input-card{
width: 26rem;
}
.input-card .btn{
margin-right: .9rem;
}
.input-card .btn:last-child{
margin-right: 0;
}
</style>
</head>
<body>
<div id="container"></div>
<div class="input-card">
<h4>获取某类覆盖物</h4>
<div class="input-item">
<input id="getPointOverlays" type="button" class="btn" value="获取点覆盖物"/>
<input id="getPolylineOverlays" type="button" class="btn" value="获取线覆盖物"/>
<input id="getPolygonOverlays" type="button" class="btn" value="获取面覆盖物"/>
</div>
</div>
<script type="text/javascript"
src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
<script type="text/javascript">
// 创建地图实例
var map = new AMap.Map("container", {
zoom: 13,
center: [116.43, 39.92],
resizeEnable: true
});
// 创建点、线、面覆盖物实例
var marker = new AMap.Marker({
position: new AMap.LngLat(116.39, 39.92),
});
var polylinePath = [
new AMap.LngLat(116.41, 39.93),
new AMap.LngLat(116.43, 39.91)
];
var polyline = new AMap.Polyline({
path: polylinePath
});
var polygonPath = [
new AMap.LngLat(116.46, 39.93),
new AMap.LngLat(116.44, 39.91),
new AMap.LngLat(116.49, 39.91)
];
var polygon = new AMap.Polygon({
path: polygonPath
});
// 将点,线,面添加到地图
map.add([marker, polyline, polygon]);
var info = {
'marker': '点标记',
'polyline': '线',
'polygon': '面',
};
function getAllOverlays(type) {
// 获取当 marker 类型的覆盖物
var overlays = map.getAllOverlays(type);
var position = type == 'marker'? overlays[0].getPosition(): overlays[0].getPath()[0];
var infoWindow = new AMap.InfoWindow({
position: position,
offset: type == 'marker'? new AMap.Pixel(0, -35): new AMap.Pixel(0, -5),
content: '<div>已获取' + info[type] + '覆盖物</div>'
});
infoWindow.open(map);
}
// 绑定获取覆盖物事件
document.getElementById("getPointOverlays").onclick = getAllOverlays.bind(null, 'marker');
document.getElementById("getPolylineOverlays").onclick = getAllOverlays.bind(null, 'polyline');
document.getElementById("getPolygonOverlays").onclick = getAllOverlays.bind(null, 'polygon');
</script>
</body>
</html>
订阅评论
0 评论
最旧