百度地圖API顯示多個標注點帶百度樣式信息檢索窗口的代碼


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>百度地圖API顯示多個標注點帶百度樣式信息檢索窗口的代碼</title>
    <!-- 原作博客地址:http://blog.csdn.net/a497785609/article/details/24009031 -->
    <!--css-->
    <link href="style/demo.css" rel="stylesheet" type="text/css" />
    <!--javascript-->
    <script src="scripts/jquery-1.9.1.js" type="text/javascript"></script>
    <script src="scripts/demo.js" type="text/javascript"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=IDvNBsejl9oqMbPF316iKsXR"></script>
<script type="text/javascript" src="http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.js"></script>
<link rel="stylesheet" href="http://api.map.baidu.com/library/SearchInfoWindow/1.5/src/SearchInfoWindow_min.css" />    
</head>
<body>
    <div class="demo_main">
        <fieldset class="demo_title">
            百度地圖API顯示多個標注點帶提示的代碼
        </fieldset>
        <fieldset class="demo_content">
            <div style="min-height: 300px; width: 100%;" id="map">
            </div>
            <script type="text/javascript">
                var markerArr = [
                    { title: "名稱:廣州火車站", point: "113.264531,23.157003", address: "廣東省廣州市廣州火車站", tel: "12306" },
                    { title: "名稱:廣州塔(赤崗塔)", point: "113.330934,23.113401", address: "廣東省廣州市廣州塔(赤崗塔) ", tel: "18500000000" },
                    { title: "名稱:廣州動物園", point: "113.312213,23.147267", address: "廣東省廣州市廣州動物園", tel: "18500000000" },
                    { title: "名稱:天河公園", point: "113.372867,23.134274", address: "廣東省廣州市天河公園", tel: "18500000000" }
                ];
                function map_init() {
                    var map = new BMap.Map("map"); // 創建Map實例
                    var point = new BMap.Point(113.312213, 23.147267); //地圖中心點,廣州市
                    map.centerAndZoom(point, 13); // 初始化地圖,設置中心點坐標和地圖級別。
                    map.enableScrollWheelZoom(true); //啟用滾輪放大縮小
                    //地圖、衛星、混合模式切換
                    map.addControl(new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP, BMAP_SATELLITE_MAP, BMAP_HYBRID_MAP]}));
                    //向地圖中添加縮放控件
                    var ctrlNav = new window.BMap.NavigationControl({
                        anchor: BMAP_ANCHOR_TOP_LEFT,
                        type: BMAP_NAVIGATION_CONTROL_LARGE
                    });
                    map.addControl(ctrlNav);
                    //向地圖中添加縮略圖控件
                    var ctrlOve = new window.BMap.OverviewMapControl({
                        anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
                        isOpen: 1
                    });
                    map.addControl(ctrlOve);
                    //向地圖中添加比例尺控件
                    var ctrlSca = new window.BMap.ScaleControl({
                        anchor: BMAP_ANCHOR_BOTTOM_LEFT
                    });
                    map.addControl(ctrlSca);
 
                    var point = new Array(); //存放標注點經緯信息的數組
                    var marker = new Array(); //存放標注點對象的數組
                    var info = new Array(); //存放提示信息窗口對象的數組
                    var searchInfoWindow =new Array();//存放檢索信息窗口對象的數組
                    for (var i = 0; i < markerArr.length; i++) {
                        var p0 = markerArr[i].point.split(",")[0]; //
                        var p1 = markerArr[i].point.split(",")[1]; //按照原數組的point格式將地圖點坐標的經緯度分別提出來
                        point[i] = new window.BMap.Point(p0, p1); //循環生成新的地圖點
                        marker[i] = new window.BMap.Marker(point[i]); //按照地圖點坐標生成標記
                        map.addOverlay(marker[i]);
                        marker[i].setAnimation(BMAP_ANIMATION_BOUNCE); //跳動的動畫
              //顯示marker的title,marker多的話可以注釋掉
                        var label = new window.BMap.Label(markerArr[i].title, { offset: new window.BMap.Size(20, -10) });
                        marker[i].setLabel(label);
                        // 創建信息窗口對象
                        info[i] = "<p style=’font-size:12px;lineheight:1.8em;’>" + "</br>地址:" + markerArr[i].address + "</br> 電話:" + markerArr[i].tel + "</br></p>";
                        //創建百度樣式檢索信息窗口對象                       
                        searchInfoWindow[i] = new BMapLib.SearchInfoWindow(map, info[i], {
                                title  : markerArr[i].title,      //標題
                                width  : 290,             //寬度
                                height : 55,              //高度
                                panel  : "panel",         //檢索結果面板
                                enableAutoPan : true,     //自動平移
                                searchTypes   :[
                                    BMAPLIB_TAB_SEARCH,   //周邊檢索
                                    BMAPLIB_TAB_TO_HERE,  //到這里去
                                    BMAPLIB_TAB_FROM_HERE //從這里出發
                                ]
                            });
                        //添加點擊事件
                        marker[i].addEventListener("click", 
                            (function(k){
                                // js 閉包
                                return function(){
                                    //將被點擊marker置為中心
                                    //map.centerAndZoom(point[k], 18);
                                    //在marker上打開檢索信息窗口
                                    searchInfoWindow[k].open(marker[k]);
                                }
                            })(i)                            
                        ); 
                    }                  
                }
                //異步調用百度js
                function map_load() {
                    var load = document.createElement("script");
                    load.src = "http://api.map.baidu.com/api?v=2.0&ak=IDvNBsejl9oqMbPF316iKsXR&callback=map_init";
                    document.body.appendChild(load);
                }
                window.onload = map_load;
            </script>
        </fieldset>
    </div>
</body>
</html>

 


免責聲明!

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



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