高德開放平台實現批量自定義marker和信息窗體顯示


上篇博客提到雲圖無法實現文本標簽標記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>

想獲取更多個人信息和相關技術內容也可以關注我的公眾號:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM