• 百度地图API 添加标签


    1.手动创建数据,实际项目则是接受GPS信息

    /建立坐标点:
    // lng:经度 lat:纬度
    var points = [  
    {"lng":112.58,"lat":26.89,"url":"http://www.baidu.com","id":1,"name":"p1"},  
    {"lng":112.59,"lat":26.90,"url":"http://www.mi.com","id":2,"name":"p2"},  
    {"lng":112.57,"lat":26.88,"url":"http://www.csdn.com","id":3,"name":"p3"}  
    ]; 

    2.创建添加标注点到地图,并为标注点添加点击事件

    //创建标注点并添加到地图中
    function addMarker(points) {
        //循环建立标注点
        for(var i=0, pointsLen = points.length; i<pointsLen; i++) {
            var point = new BMap.Point(points[i].lng, points[i].lat); //将标注点转化成地图上的点
            var marker = new BMap.Marker(point); //将点转化成标注点
            map.addOverlay(marker);  //将标注点添加到地图上
            //添加监听事件
            (function() {
                var thePoint = points[i];
                marker.addEventListener("click",
                //显示信息的方法
                    function() {
                    showInfo(this,thePoint);
                });
             })();  
        }
    }

    3.编写信息显示方法

    function showInfo(thisMarker,point) {
        //获取点的信息
        var sContent = 
        '<ul style="margin:0 0 5px 0;padding:0.2em 0">'  
        +'<li style="line-height: 26px;font-size: 15px;">'  
        +'<span style=" 50px;display: inline-block;">id:</span>' + point.id + '</li>'  
        +'<li style="line-height: 26px;font-size: 15px;">'  
        +'<span style=" 50px;display: inline-block;">名称:</span>' + point.name + '</li>'  
        +'<li style="line-height: 26px;font-size: 15px;"><span style=" 50px;display: inline-block;">查看:</span><a href="'+point.url+'">详情</a></li>'  
        +'</ul>';
        var infoWindow = new BMap.InfoWindow(sContent); //创建信息窗口对象
        thisMarker.openInfoWindow(infoWindow); //图片加载完后重绘infoWindow
    }

    4.创建地图

    //创建地图
    var map = new BMap.Map("allmap");    
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);  // 设置中心点
    map.centerAndZoom( "衡阳");       //指定中心城市
    map.setCurrentCity("衡阳");          
    map.addControl(new BMap.MapTypeControl());  //设置可拖拽 
    map.enableScrollWheelZoom(true);  //添加滚轮缩放   
    addMarker(points); //添加标注
    </script>

    Tip: 想使用百度地图api需要申请一个ak,申请方法就不介绍了,百度一大堆.

    1.5 版本 完整代码(不包括api的ak 需要自己申请):

    <!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,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
    </style>
    <script type="text/javascript" 
    //ak填自己申请的ak
    src="http://api.map.baidu.com/api?v=2.0& ak=你申请的AK">
    </script>
    <title>百度地图api展示</title>
    </head>
    <body>
    <div id="allmap"></div>
    </body>
    </html>
    <script type="text/javascript">
    //新建三个地图上点
    var points = [  
    {"lng":112.58,"lat":26.89,"url":"http://www.baidu.com","id":1,"name":"p1"},  
    {"lng":112.59,"lat":26.90,"url":"http://www.mi.com","id":2,"name":"p2"},  
    {"lng":112.57,"lat":26.88,"url":"http://www.csdn.com","id":3,"name":"p3"}  
    ]; 
    //创建标注点并添加到地图中
    function addMarker(points) {
        //循环建立标注点
        for(var i=0, pointsLen = points.length; i<pointsLen; i++) {
            var point = new BMap.Point(points[i].lng, points[i].lat); //将标注点转化成地图上的点
            var marker = new BMap.Marker(point); //将点转化成标注点
            map.addOverlay(marker);  //将标注点添加到地图上
            //添加监听事件
            (function() {
                var thePoint = points[i];
                marker.addEventListener("click",
                    function() {
                    showInfo(this,thePoint);
                });
             })();  
        }
    }
    function showInfo(thisMarker,point) {
        //获取点的信息
        var sContent = 
        '<ul style="margin:0 0 5px 0;padding:0.2em 0">'  
        +'<li style="line-height: 26px;font-size: 15px;">'  
        +'<span style=" 50px;display: inline-block;">id:</span>' + point.id + '</li>'  
        +'<li style="line-height: 26px;font-size: 15px;">'  
        +'<span style=" 50px;display: inline-block;">名称:</span>' + point.name + '</li>'  
        +'<li style="line-height: 26px;font-size: 15px;"><span style=" 50px;display: inline-block;">查看:</span><a href="'+point.url+'">详情</a></li>'  
        +'</ul>';
        var infoWindow = new BMap.InfoWindow(sContent); //创建信息窗口对象
        thisMarker.openInfoWindow(infoWindow); //图片加载完后重绘infoWindow
    }
    //创建地图
    var map = new BMap.Map("allmap");    
    map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);  // 设置中心点
    map.centerAndZoom( "衡阳");
    map.setCurrentCity("衡阳");          //设置为衡阳
    map.addControl(new BMap.MapTypeControl());   
    map.enableScrollWheelZoom(true);     
    addMarker(points);
    </script>

    第二种写法

    <!DOCTYPE html>  
    <html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
        <title>添加标注点-百度地图API</title>
        <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=YWdGplhYjUGQ3GtpKNeuTM2S"></script>
    </head>  
    
    <body>
        <style type="text/css">  
            html{height:100%}  
            body{height:100%;margin:0 10px;}  
            #container{height:100%;margin: 20px;} 
            .info_ul{
                margin:0 0 5px 0;
                padding:0.2em 0;
            } 
            .info_li{
                line-height: 26px;font-size: 15px;
            }
            .info_span{
                width: 50px;display: inline-block;
            }
        </style>    
        <div id="container"></div> 
        <script type="text/javascript"> 
            var map = new BMap.Map("container"); //初始化地图
    
            var points = [
            {"lng":116,"lat":40,"url":"http://www.baidu.com","id":50,"name":"p1"},
            {"lng":117,"lat":31,"url":"http://www.taobao.com","id":2,"name":"p2"},
            {"lng":116,"lat":34,"url":"http://www.qq.com","id":3,"name":"p3"}
            ];//数据准备
    
            map.centerAndZoom(new BMap.Point(103.388611,35.563611), 5);//设置中心点和显示级别。中国。
            
            map.enableScrollWheelZoom();//滚轮放大缩小。
            
            addMarker(points);//添加标注。
    
            function addMarker(points){  // 创建图标对象   
                var point,marker;
                // 创建标注对象并添加到地图   
                for(var i = 0,pointsLen = points.length;i <pointsLen;i++){
                    point = new BMap.Point(points[i].lng,points[i].lat);    
                    marker = new BMap.Marker(point);   
                    map.addOverlay(marker); 
                    //给标注点添加点击事件。使用立即执行函数和闭包
                    (function() {
                        var thePoint = points[i];
                        marker.addEventListener("click",function(){
                            showInfo(this,thePoint);
                        });
                    })();
                }
            }
    
            //显示信息窗口,显示标注点的信息。
            function showInfo(thisMaker,point){
                var sContent =
                '<ul class="info_ul">'
                +'<li class="info_li">'
                +'<span class="info_span">id:</span>' + point.id + '</li>'
                +'<li class="info_li">'
                +'<span class="info_span">名称:</span>' + point.name + '</li>'
                +'<li class="info_li"><span class="info_span">查看:</span><a href="'+point.url+'" target="_blank">详情</a></li>'
                +'</ul>';
                var infoWindow = new BMap.InfoWindow(sContent);// 创建信息窗口对象
                   thisMaker.openInfoWindow(infoWindow);//图片加载完毕重绘infowindow
               }
    
            </script>  
        </body>  
        </html>

    1.2版本和1.5版本通用,但是不用申请ak

    <!doctype html>
    <html>
    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
        <title>Baidu Map</title>
        <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2&services=true">
        </script>
    </head>
    <body>
        <div id="container" style=" 800px; height: 600px;">
        </div>
    </body>
    </html>
    <script type="text/javascript">
    
        var map = new BMap.Map("container"); //初始化地图
    
        var points = [
        { "lng": 121.653872, "lat": 38.928724, "url": "http://www.baidu.com", "id": 1, "name": "麦当劳" },
        { "lng": 121.653234, "lat": 38.928604, "url": "http://www.taobao.com", "id": 2, "name": "天天鱼巷" },
        { "lng": 121.64576, "lat": 38.929096, "url": "http://www.qq.com", "id": 3, "name": "好利来" },
        { "lng": 121.654402, "lat": 38.926787, "url": "http://www.qq.com", "id": 4, "name": "金铭烤肉" },
        { "lng": 121.647629, "lat": 38.927369, "url": "http://www.qq.com", "id": 5, "name": "嘉禾粥道" },
        { "lng": 121.646667, "lat": 38.925264, "url": "http://www.qq.com", "id": 6, "name": "彤德莱" },
        { "lng": 121.644799, "lat": 38.928085, "url": "http://www.qq.com", "id": 7, "name": "鼎香橼自助" },
        { "lng": 121.646209, "lat": 38.929818, "url": "http://www.qq.com", "id": 3, "name": "风情丽江斑鱼火锅" }
        ];//数据准备
    
        map.centerAndZoom(new BMap.Point(121.650467, 38.927376), 16);//设置中心点和显示级别。中国。
    
        map.enableScrollWheelZoom();//滚轮放大缩小。
    
        addMarker(points);//添加标注。
    
        function addMarker(points){  // 创建图标对象
            var point,marker;
            // 创建标注对象并添加到地图
            for(var i = 0,pointsLen = points.length;i <pointsLen;i++){
                point = new BMap.Point(points[i].lng,points[i].lat);
                marker = new BMap.Marker(point);
                map.addOverlay(marker);
                //给标注点添加点击事件。使用立即执行函数和闭包
                (function() {
                    var thePoint = points[i];
                    marker.addEventListener("click",function(){
                        showInfo(this,thePoint);
                    });
                })();
            }
        }
    
        //显示信息窗口,显示标注点的信息。
        function showInfo(thisMaker,point){
            var sContent =
            '<ul class="info_ul">'
            +'<li class="info_li">'
            +'<span class="info_span">id:</span>' + point.id + '</li>'
            +'<li class="info_li">'
            +'<span class="info_span">名称:</span>' + point.name + '</li>'
            +'<li class="info_li"><span class="info_span">查看:</span><a href="'+point.url+'" target="_blank">详情</a></li>'
            +'</ul>';
            var infoWindow = new BMap.InfoWindow(sContent);// 创建信息窗口对象
            thisMaker.openInfoWindow(infoWindow);//图片加载完毕重绘infowindow
        }
    
    </script>
  • 相关阅读:
    如何用正确的方法写出高质量软件的75条体会(转)
    使用javascript动态添加onclick事件,
    签名和重载
    C#文件后缀名详解
    配置SQL Server 2005 Express的身份验证方式,以及如何启用sa登录名。
    CSS选择符及优先级计算
    关于软件版本的解释
    数据结构形象解释
    CSS属性选择符
    [转载]Repeater三层嵌套
  • 原文地址:https://www.cnblogs.com/ye-ming/p/8072080.html
Copyright © 2020-2023  润新知