天天看點

高德地圖添加Marker點标記

示例代碼部分:

<!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="http://cache.amap.com/lbs/static/main1119.css"/>
    <style>
        .marker {
            color: #ff6600;
            padding: px px;
            border: px solid #fff;
            white-space: nowrap;
            font-size: px;
            font-family: "";
            background-color: #0066ff;
        }
    </style>
    <script src="http://webapi.amap.com/maps?v=1.3&key=您申請的key值"></script>
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div id="container"></div>
<div  class="button-group">
    <input type="button" class="button" value="添加點标記覆寫物" id="addMarker"/>
    <input type="button" class="button" value="更新點标記覆寫物" id="updateMarker"/>
    <input type="button" class="button" value="删除點标記覆寫物" id="clearMarker"/>
</div>
<script>
    var marker, map = new AMap.Map("container", {
        resizeEnable: true,
        center: [,],
        zoom: 
    });
    AMap.event.addDomListener(document.getElementById('addMarker'), 'click', function() {
        addMarker();
    }, false);
    AMap.event.addDomListener(document.getElementById('updateMarker'), 'click', function() {
        marker && updateMarker();
    }, false);
    AMap.event.addDomListener(document.getElementById('clearMarker'), 'click', function() {
        if (marker) {
            marker.setMap(null);
            marker = null;
        }
    }, false);

    // 執行個體化點标記
    function addMarker() {
        if (marker) {
            return;
        }
        marker = new AMap.Marker({
            icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position: [,]
        });
        marker.setMap(map);
    }

    function updateMarker() {
        // 自定義點标記内容
        var markerContent = document.createElement("div");

        // 點标記中的圖示
        var markerImg = document.createElement("img");
        markerImg.className = "markerlnglat";
        markerImg.src = "http://webapi.amap.com/theme/v1.3/markers/n/mark_r.png";
        markerContent.appendChild(markerImg);

        // 點标記中的文本
        var markerSpan = document.createElement("span");
        markerSpan.className = 'marker';
        markerSpan.innerHTML = "Hi,我換新裝備啦!";
        markerContent.appendChild(markerSpan);

        marker.setContent(markerContent); //更新點标記内容
        marker.setPosition([,]); //更新點标記位置
    }
</script>
</body>
</html>
           
  • 代碼片分析:

核心部分,如何添加一個點?

高德地圖添加Marker點标記

實作基本功能的代碼:

//這裡是設定地圖中心點,比如這裡選了一個綿陽為中心的坐标
 var marker, map = new AMap.Map("container", {
        resizeEnable: true,
        center: [,],
        zoom: 
    });

    window.onload = function(){
        addMarker();
    }
    // 執行個體化點标記
    function addMarker() {
        if (marker) {
            return;
        }
        marker = new AMap.Marker({
            icon: "http://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
            position: [,]//在這裡設定需要打點的坐标
        });
        marker.setMap(map);
    }
           

附加:怎麼添加擷取地圖上的坐标點?高德地圖工具

http://lbs.amap.com/console/show/picker

高德地圖添加Marker點标記

講擷取的店坐标拷貝到示例代碼此處:

高德地圖添加Marker點标記

儲存,運作,地圖上出現Marker點

繼續閱讀