十八 高德地圖入門

口扣技術(shù)交流 q u n : 894441210

合作vx:xuexiv5876

ECharts地圖

高德開發(fā)者賬號(hào)申請(qǐng)

基本用法

包括:

  • 加載地圖
  • 顯示圖層
  • 繪制圖標(biāo)
  • 繪制矢量圖
  • 編輯矢量圖
  • 繪制窗口
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
    body, html,#container {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微軟雅黑";}
    #container {position: relative;}
    #tools {position: absolute;right:10px;top:10px;}
    .amap-logo {display: none!important;}
    .amap-copyright {display: none!important;}
    .marker {
        position: absolute;
        top: 0px;
        right: 0px;
        color: #fff;
        padding: 4px 10px;
        box-shadow: 1px 1px 1px rgba(10, 10, 10, .2);
        white-space: nowrap;
        font-size: 12px;
        font-family: "";
        background-color: #25A5F7;
        border-radius: 3px;
    }
    </style>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=e8653883477c1e38d190463340771816&plugin=AMap.PolyEditor"></script>
    <title>地圖展示</title>
</head>
<body>
    <div id="container"></div>
    <div id="tools">
        <div>
            <button id="show-traf">顯示交通</button>
            <button id="hide-traf">隱藏交通</button>
        </div>
        <div>
            <button id="show-sate">顯示衛(wèi)星</button>
            <button id="hide-sate">隱藏衛(wèi)星</button>
        </div>
        <div>
            <button id="show-road">顯示道路</button>
            <button id="hide-road">隱藏道路</button>
        </div>
        <div>
            <button id="show-point">繪制點(diǎn)</button>
            <button id="hide-point">隱藏點(diǎn)</button>
        </div>
        <div>
            <button id="show-text">繪制文本</button>
            <button id="hide-text">隱藏文本</button>
        </div>
        <div>
            <button id="show-line">繪制線條</button>
            <button id="hide-line">隱藏線條</button>
        </div>
        <div>
            <button id="start-edit-line">編輯線條</button>
            <button id="close-edit-line">停止編輯</button>
        </div>
        <div>
            <button id="show-circle">繪制圓圈</button>
            <button id="hide-circle">隱藏圓圈</button>
        </div>
        <div>
            <button id="show-info">繪制窗口</button>
            <button id="hide-info">隱藏窗口</button>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    var sateLayer = new AMap.TileLayer.Satellite(),
        roadLayer = new AMap.TileLayer.RoadNet(),
        trafficLayer = new AMap.TileLayer.Traffic({
            zIndex: 10
        });
  var map = new AMap.Map('container', {
        zoom: 11, //級(jí)別
        zooms: [8, 15],
        // layers: [
        //  sateLayer,
        //  roadLayer
        // ],
        center: [116.397428, 39.90923], //中心點(diǎn)坐標(biāo)
        viewMode: '3D' //使用3D視圖
    });
    AMap.plugin(['AMap.ToolBar','AMap.Scale'], function(){//異步加載插件
        var toolbar = new AMap.ToolBar();
        map.addControl(toolbar);
        var scale = new AMap.Scale({
            offset: new AMap.Pixel(10, 10)
        });
        map.addControl(scale);
    });
    var marker = new AMap.Marker({
        icon: "https://a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
        position: [116.406315,39.908775],
        offset: new AMap.Pixel(-13, -30)
    });
    var contentMarker = new AMap.Marker({
        position: [116.406315,39.908775],
        offset: new AMap.Pixel(130, 0)
    });
    var markerContent = document.createElement("div");
    var markerSpan = document.createElement("span");
    markerSpan.className = 'marker';
    markerSpan.innerHTML = "test!";
    markerContent.appendChild(markerSpan);

    var lineArr = [
        [116.368904, 39.913423],
        [116.382122, 39.901176],
        [116.387271, 39.912501],
        [116.398258, 39.904600]
    ];
    var polyline = new AMap.Polyline({
        path: lineArr,          //設(shè)置線覆蓋物路徑
        strokeColor: "#3366FF", //線顏色
        strokeWeight: 5,        //線寬
        strokeStyle: "solid",   //線樣式
    });

    var infoMarker = new AMap.Marker({
        position: [116.481181, 39.989792]
    });
    var infoWindow = new AMap.InfoWindow({ //創(chuàng)建信息窗體
        isCustom: true,  //使用自定義窗體
        content:'<div style="color:red;">信息窗體</div>', //信息窗體的內(nèi)容可以是任意html片段
        offset: new AMap.Pixel(16, -45)
    });
    var onMarkerClick = function(e) {
        infoWindow.open(map, e.target.getPosition());//打開信息窗體
    };
    infoMarker.on('click', onMarkerClick); //綁定click事件

    var polyEditor = new AMap.PolyEditor(map, polyline)

    var circle = new AMap.Circle({
    center: new AMap.LngLat(116.39, 39.9),  // 圓心位置
    radius: 1000, // 圓半徑
    fillColor: 'red',   // 圓形填充顏色
    strokeColor: '#fff', // 描邊顏色
    strokeWeight: 2 // 描邊寬度
    });

    document.getElementById('show-traf').addEventListener('click', function(e){
    map.add(trafficLayer); //添加圖層到地圖
        trafficLayer.show();
    });
    document.getElementById('hide-traf').addEventListener('click', function(e){
    trafficLayer.hide();
    });
    document.getElementById('show-sate').addEventListener('click', function(e){
    map.add(sateLayer); //添加圖層到地圖
        sateLayer.show();
    });
    document.getElementById('hide-sate').addEventListener('click', function(e){
    sateLayer.hide();
    });
    document.getElementById('show-road').addEventListener('click', function(e){
    map.add(roadLayer); //添加圖層到地圖
        roadLayer.show();
    });
    document.getElementById('hide-road').addEventListener('click', function(e){
    roadLayer.hide();
    });
    document.getElementById('show-point').addEventListener('click', function(e){
    map.add(marker);
        // marker.setMap(map);
    });
    document.getElementById('hide-point').addEventListener('click', function(e){
    map.remove(marker);
    });
    document.getElementById('show-text').addEventListener('click', function(e){
    contentMarker.setMap(map);
        contentMarker.setContent(markerContent); //更新點(diǎn)標(biāo)記內(nèi)容
        contentMarker.setPosition([116.391467, 39.927761]); //更新點(diǎn)標(biāo)記位置
    });
    document.getElementById('hide-text').addEventListener('click', function(e){
        map.remove(contentMarker);
    });
    document.getElementById('show-line').addEventListener('click', function(e){
    map.add(polyline);
    });
    document.getElementById('hide-line').addEventListener('click', function(e){
        map.remove(polyline);
    });
    document.getElementById('start-edit-line').addEventListener('click', function(e){
    polyEditor.open();
    });
    document.getElementById('close-edit-line').addEventListener('click', function(e){
        polyEditor.close();
    });
    document.getElementById('show-circle').addEventListener('click', function(e){
    map.add(circle);
    });
    document.getElementById('hide-circle').addEventListener('click', function(e){
        map.remove(circle);
    });
    document.getElementById('show-info').addEventListener('click', function(e){
        map.add(infoMarker);
    });
    document.getElementById('hide-info').addEventListener('click', function(e){
        map.remove(infoMarker);
        infoWindow.close();
    });
</script>

異步加載

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
    body, html,#container {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微軟雅黑";}
    #container {position: relative;}
    #tools {position: absolute;left:10px;top:10px;}
    </style>
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=e8653883477c1e38d190463340771816"></script>
    <title>地圖展示</title>
</head>
<body>
    <div id="container"></div>
    <div id="tools">
        <div>
            <button id="show-traf">顯示交通</button>
            <button id="hide-traf">隱藏交通</button>
        </div>
        <div>
            <button id="show-sate">顯示衛(wèi)星</button>
            <button id="hide-sate">隱藏衛(wèi)星</button>
        </div>
        <div>
            <button id="show-road">顯示道路</button>
            <button id="hide-road">隱藏道路</button>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    window.init = init;
    var url = 'https://webapi.amap.com/maps?v=1.4.15&key=e8653883477c1e38d190463340771816&callback=init';
    var jsapi = document.createElement('script');
    jsapi.charset = 'utf-8';
    jsapi.src = url;
    document.head.appendChild(jsapi);
    function init() {
        var sateLayer = new AMap.TileLayer.Satellite(),
            roadLayer = new AMap.TileLayer.RoadNet(),
            trafficLayer = new AMap.TileLayer.Traffic({
                zIndex: 10
            });
        var map = new AMap.Map('container', {
            zoom: 11, //級(jí)別
            zooms: [8, 12],
            center: [116.397428, 39.90923], //中心點(diǎn)坐標(biāo)
            viewMode: '3D' //使用3D視圖
        });
        document.getElementById('show-traf').addEventListener('click', function(e){
            map.add(trafficLayer); //添加圖層到地圖
            trafficLayer.show();
        });
        document.getElementById('hide-traf').addEventListener('click', function(e){
            trafficLayer.hide();
        });
        document.getElementById('show-sate').addEventListener('click', function(e){
            map.add(sateLayer); //添加圖層到地圖
            sateLayer.show();
        });
        document.getElementById('hide-sate').addEventListener('click', function(e){
            sateLayer.hide();
        });
        document.getElementById('show-road').addEventListener('click', function(e){
            map.add(roadLayer); //添加圖層到地圖
            roadLayer.show();
        });
        document.getElementById('hide-road').addEventListener('click', function(e){
            roadLayer.hide();
        });
    }
</script>

自定義彈窗

<!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>默認(rèn)樣式信息窗體</title>
    <link rel="stylesheet" />
    <style>
        html, body, #container {
            height: 100%;
            width: 100%;
        }

        .content-window-card {
            position: relative;
            box-shadow: none;
            bottom: 0;
            left: 0;
            width: auto;
            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;
        }
    </style>
</head>
<body>
<div id="container"></div>
<div class="info">
    點(diǎn)擊地圖上的點(diǎn)標(biāo)記,打開所添加的自定義信息窗體
</div>
<script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.15&key=您申請(qǐng)的key值"></script>
<script type="text/javascript">    //地圖初始化時(shí),在地圖上添加一個(gè)marker標(biāo)記,鼠標(biāo)點(diǎn)擊marker可彈出自定義的信息窗體
var map = new AMap.Map("container", {
    resizeEnable: true,
    center: [118.784902,32.044077],
    zoom: 16
});
addMarker();

//添加marker標(biāo)記
function addMarker() {
    map.clearMap();
    var marker = new AMap.Marker({
            map: map,
            position: [118.784902,32.044077]
    });
    //鼠標(biāo)點(diǎn)擊marker彈出自定義的信息窗體
    AMap.event.addListener(marker, 'click', function () {
            infoWindow.open(map, marker.getPosition());
    });
}

//實(shí)例化信息窗體
var title = '南京德基廣場(chǎng)<span style="font-size:11px;color:#F00;">周末促銷</span>',
    content = [];
content.push("<img src='https://bkimg.cdn.bcebos.com/pic/962bd40735fae6cdc054821d03b30f2443a70fed?x-bce-process=image/resize,m_lfit,w_268,limit_1/format,f_jpg' style='width:200px;height:140px'>地址:南京市玄武區(qū)中山路18號(hào)");
content.push("電話:025-88888888");
content.push("<a );
var infoWindow = new AMap.InfoWindow({
    isCustom: true,  //使用自定義窗體
    content: createInfoWindow(title, content.join("<br/>")),
    offset: new AMap.Pixel(16, -45)
});

//構(gòu)建自定義信息窗體
function createInfoWindow(title, content) {
    var info = document.createElement("div");
    info.className = "custom-info input-card content-window-card";

    //可以通過下面的方式修改自定義窗體的寬高
    //info.style.width = "400px";
    // 定義頂部標(biāo)題
    var top = document.createElement("div");
    var titleD = document.createElement("div");
    var closeX = document.createElement("img");
    top.className = "info-top";
    titleD.innerHTML = title;
    closeX.src = "https://webapi.amap.com/images/close2.gif";
    closeX.onclick = closeInfoWindow;

    top.appendChild(titleD);
    top.appendChild(closeX);
    info.appendChild(top);

    // 定義中部?jī)?nèi)容
    var middle = document.createElement("div");
    middle.className = "info-middle";
    middle.style.backgroundColor = 'white';
    middle.innerHTML = content;
    info.appendChild(middle);

    // 定義底部?jī)?nèi)容
    var bottom = document.createElement("div");
    bottom.className = "info-bottom";
    bottom.style.position = 'relative';
    bottom.style.top = '0px';
    bottom.style.margin = '0 auto';
    var sharp = document.createElement("img");
    sharp.src = "https://webapi.amap.com/images/sharp.png";
    bottom.appendChild(sharp);
    info.appendChild(bottom);
    return info;
}

//關(guān)閉信息窗體
function closeInfoWindow() {
    map.clearInfoWindow();
}
</script>
</body>
</html>

數(shù)據(jù)可視化應(yīng)用場(chǎng)景

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容