地圖自定義彈窗 高德地圖信息窗體 鼠標點擊地圖點標記顯示彈窗html


高德地圖

vue使用:https://www.cnblogs.com/luckybaby519/p/15706546.html

HTML使用:https://www.cnblogs.com/luckybaby519/p/15703467.html

效果圖如下所示:

 

 html頁面完整代碼如下:(圖片路徑自己自定義)

<!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%;
            }
            .content-window-card {
                position: relative;
                box-shadow: none;
                bottom: 0;
                left: 0;
                /* width: auto; */
                width: 28rem;
                padding: 0;
            }

            .content-window-card p {
                height: 2rem;
            }

            .custom-info {
                border: solid 1px silver;
            }

            div.info-top {
                position: relative;
                background: none repeat scroll 0 0 #F9F9F9;
                border-bottom: 1px solid #CCC;
                border-radius: 5px 5px 0 0;
            }

            div.info-top div {
                display: inline-block;
                color: #333333;
                font-size: 14px;
                font-weight: bold;
                line-height: 31px;
                padding: 0 10px;
            }

            div.info-top img {
                position: absolute;
                top: 10px;
                right: 10px;
                transition-duration: 0.25s;
            }

            div.info-top img:hover {
                box-shadow: 0px 0px 5px #000;
            }

            div.info-middle {
                font-size: 12px;
                padding: 10px 6px;
                line-height: 20px;
            }

            div.info-bottom {
                height: 0px;
                width: 100%;
                clear: both;
                text-align: center;
            }

            div.info-bottom img {
                position: relative;
                z-index: 104;
            }

            span {
                margin-left: 5px;
                font-size: 11px;
            }

            .info-middle img {
                float: left;
                margin-right: 6px;
            }

            .info-span {
                /* margin-left: 35px; */
                font-size: 11px;

            }

            .info-div {
                width: 140px;
                display: inline-block;
                margin-left: 10px;
            }

            .info-img {
                width: 40px;
                height: 40px;
            }

            .info-a-title {
                /* color: #000000; */
                font-size: 16px;
            }
        </style>
    </head>
    <body>
        <div id="container"></div>
        <div class="info">
            點擊地圖上的點標記,打開所添加的自定義信息窗體
        </div>
          <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申請的key值">
        </script>
        <script type="text/javascript">
            var devInfo = null
            var map = createMap() //1.new 一個map對象
            map.clearMap(); //2.清空Map
            addMarker() //3.創建點標記
            //1創建map對象,
            function createMap() {
                //1.地圖初始化時,在地圖上添加一個marker標記,鼠標點擊marker可彈出自定義的信息窗體
                var mapData = new AMap.Map("container", {
                    resizeEnable: true,
                    center: [108.9768933198242, 34.24637248278447], //地圖展示中心點位置
                    zoom: 16
                });
                return mapData
            }
            //2添加點標記
            function addMarker() {
                var markerData = [{
                    title: '1號廠區',
                    icon: "img/gc2.png", //點標記圖片路徑
                    position: [108.971642, 34.247119],
                    offset: new AMap.Pixel(-8, -30)
                }, {
                    title: '2號廠區',
                    icon: "img/gc2.png", //點標記圖片路徑
                    position: [108.9768933198242, 34.24537248278517],
                    offset: new AMap.Pixel(-3, -30)
                }, {
                    title: '3號廠區',
                    icon: "img/gc2.png", //點標記圖片路徑
                    position: [108.979965, 34.250659],
                    offset: new AMap.Pixel(-12, -30)
                }]
                var arr = []
                markerData.forEach((item) => {
                    var marker = new AMap.Marker({
                        icon: item.icon, //點標記圖片路徑
                        position: item.position, //位置
                        offset: item.offset //偏移
                    });
                      arr.push(Object.assign(item, {
                        mapId: marker._amap_id
                    }))
                    marker.setMap(map);
                    // 鼠標點擊marker彈出自定義的信息窗體

                    AMap.event.addListener(marker, 'click', function(e) {
                        markerClick(arr, marker)
                    });
                })
            }
            //點擊標記 獲取所點擊標記的信息以及窗體要展示的數據,創建信息窗體
            function markerClick(arr, marker) {
                var arrNew = arr.filter(x => x.mapId == marker._amap_id)

                devInfo = arrNew && arrNew[0]
                var infoWindow = createInfoWindow()
                openInfoWindow(infoWindow, marker)
            }
            //構建自定義窗體
            function createInfoWindow() {
                var infoWindowData = new AMap.InfoWindow({
                    isCustom: true, //使用自定義窗體
                    content: getContent(),
                    offset: new AMap.Pixel(16, -45)
                });
                return infoWindowData
            }
            //處理窗體內容
            function getContent() {
                var content = `<div class="custom-info input-card content-window-card">
                <div class="info-top">
                    <div><span>${devInfo.title}</span><span style="font-size:11px; margin-left:20px;color:#F00;">狀態:正在運行</span></div><img
                      onclick="closeInfoWindow"  src="https://webapi.amap.com/images/close2.gif">
                </div>
                <div class="info-middle" style="background-color: white;"><img class="info-img" src="img/dev.png"><a
                        class="info-a-title"
                        href="https://ditu.amap.com/detail/B000A8URXB?citycode=110105">XXXXXXXXXXX</a><br>地址:XXXXXXXXXXXXXXXXXXXXXXX<br>
                    <div class="info-div">總用電:5428542° </div><span class="info-span"> 總用氣:454575NM<br>
                        <div class="info-div">發酵罐:210個 </div><span class="info-span"> 總用水量:19999111T</span><br>
                        <div class="info-div">工作年限:10年</div> <span class="info-span"> 總產量:27784T</span><br>
                        <div class="info-div">建廠時間:2011.09.08</div> <span class="info-span">建築面積:1200M3</span>
                    </span>
                </div>
                <div class="info-bottom" style="position: relative; top: 0px; margin: 0px auto;"><img
                        src="https://webapi.amap.com/images/sharp.png"></div>
            </div>`
                return content
            }
            //打開窗體
            function openInfoWindow(infoWindow, marker) {
                debugger
                infoWindow.open(map, marker.getPosition());
            }
            //關閉窗體
            function closeInfoWindow() {
                map.clearInfoWindow();
            }
        </script>
    </body>
</html>

 


免責聲明!

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



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