上篇博客提到云图无法实现文本标签标记marker,这篇博客着重实现在marker点文本标记以及自定义按钮窗体显示。
1.效果:
2.代码实现
<!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>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.14&key=760d16c25fa8ee3c547b693a6c414821&plugin=AMap.DistrictSearch,Map3D"></script>
<script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
<style>
html,
body,
#container {
margin: 0;
height: 100%;
}
</style>
</head>
<body>
<div id="container"></div>
<script>
var p;//定义景点坐标数组
var markers = [];//记录所有景点的Marker信息
var _location;
var map = new AMap.Map('container', {
resizeEnable: true,
zoom: 12,
viewMode: '3D',
center: [121.124178, 31.150681],
mapStyle: 'amap://styles/5a2dbb143362de08809a8aebe25ca455',
//layers: [
// new AMap.TileLayer.RoadNet({
// zIndex: 20
// })]//,
// new AMap.TileLayer({
// zIndex: 6,
// opacity: 1,
//getTileUrl: 'https://t{1,2,3,4}.tianditu.gov.cn/DataServer?T=ter_w&x=[x]&y=[y]&l=[z]'
//getTileUrl: 'https://t{s}.tianditu.gov.cn/vec_w/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=vec&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TILEMATRIX={z}&TILEROW={y}&TILECOL={x}&tk=5ecfe4e0cecdafec9a858e37c261084c'
//})]
});
loadScenic()
addmarker()
new AMap.DistrictSearch({
extensions: 'all',
subdistrict: 0
}).search('青浦区', function (status, result) {
// 外多边形坐标数组和内多边形坐标数组
var outer = [
new AMap.LngLat(-360, 90, true),
new AMap.LngLat(-360, -90, true),
new AMap.LngLat(360, -90, true),
new AMap.LngLat(360, 90, true),
];
var holes = result.districtList[0].boundaries
var pathArray = [
outer
];
pathArray.push.apply(pathArray, holes)
var polygon = new AMap.Polygon({
pathL: pathArray,
strokeColor: '#00eeff',
strokeWeight: 1,
fillColor: '#71B3ff',
fillOpacity: 0.5
});
polygon.setPath(pathArray);
map.add(polygon);
var bounds = map.getBounds(); // 获取显示范围
map.setLimitBounds(bounds); // 限制地图显示范围
});
function addmarker() {
//var markers = [];
for (var i = 0; i < p.length; i++) {
var point=[p[i].x,p[i].y]
var marker = new AMap.Marker({
position: point,
map: map,
title: p[i].title,
address: p[i].address,
icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png",
visible: true,
});
marker.setLabel({
offset: new AMap.Pixel(-60, -40), //设置文本标注偏移量
content: p[i].title, //设置文本标注内容
direction: 'right' //设置文本标注方位
});
AMap.event.addListener(marker, 'click', function (e) {
var clouddata = e.target.He;
_location = [clouddata.position.lng, clouddata.position.lat];
//var photo = ['<img width=240 height=100 src="' + clouddata.img + '"><br>'];
var infoWindow = new AMap.InfoWindow({
content: "<font class='title'>" + "名称:" + clouddata.title + "<br />" + "地址:" + clouddata.address + "<br />" + "联系电话:" + "021-69237067" + "<br />" + "经纬度:" + _location,
size: new AMap.Size(0, 0),
autoMove: true,
offset: new AMap.Pixel(0, -25)
});
infoWindow.open(map, _location);
console.log(clouddata);
});
markers.push(marker);
}
//map.setFitView();
}
function loadScenic() {
p = [{ x: 121.185251, y: 31.197498, title: '赵重路泵站', address: '上海市青浦区赵重公路2396号'},
{ x: 121.131239, y: 31.17475, title: '通波塘泵站', address: '上海市青浦区淞泽大道888号' },
{ x: 121.130353, y: 31.166507, title: '华青路泵站', address: '上海市青浦区华青路666号' },
{ x: 121.151442, y: 31.171122, title: '新业路泵站', address: '上海市青浦区汇金路999号' },
{ x: 121.117217, y: 31.175185, title: '新区路泵站', address: '上海市青浦区淞泽大道9502号' },
{ x: 121.211852, y: 31.181424, title: '赵巷A泵站', address: '上海市青浦区嘉松中路999号' },
{ x: 121.209637, y: 31.172491, title: '赵巷B泵站', address: '上海市青浦区青浦区芦沈路125号' },
{ x: 121.228863, y: 31.165094, title: '赵巷C泵站', address: '上海市青浦区盈港东路999号青山支路' },
{ x: 121.090117, y: 31.19679, title: '金星路泵站', address: '上海市青浦区天辰路888号' },
{ x: 121.140076, y: 31.141699, title: '外青松泵站', address: '上海市青浦区外青松公路888号' },
{ x: 121.09622, y: 31.203285, title: '新金路泵站', address: '上海市青浦区新金路888号' },
{ x: 121.151911, y: 31.153891, title: '汇金路泵站', address: '上海市青浦区汇金路秀泽路' },
{ x: 121.080865, y: 31.174616, title: '民乐路泵站', address: '上海市青浦区淞泽大道西青赵公路' },
{ x: 121.151442, y: 31.171122, title: '新城一站泵站', address: '上海市青浦区汇金路999号' },
{ x: 1121.137333, y: 31.166801, title: '青浦第二污水厂', address: '上海市青浦区新水路1号' },
];
}
</script>
</body>
</html>
想获取更多个人信息和相关技术内容也可以关注我的公众号:
手机扫一扫
移动阅读更方便
你可能感兴趣的文章