易站源码-提供最新免费网站源码下载、源码交易与技术服务!

百度地图api标注点和搜索

  • 时间:2017-07-18 20:47 来源: 阅读:616
  • 扫一扫,手机访问
摘要:代码: <html> <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />     <meta n

百度地图api标注点和搜索

代码:

 

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=密钥"></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" />    
    <style type="text/css">
        body, html{width: 100%;height: 100%; margin:0;font-family:"微软雅黑";}        
        #r-result{width:20%;height:90%}
        #map{height:100%;width:79.9%;}
    </style>
</head>
<body>
        <div>
            <input type="text" onkeypress="EnterPress()" style="width:16%;height:25px" id="txtContent">
            <input type="button" style="width:50px;height:25px" id="search" value="搜索" onclick="searchMap()"/>
        </div><br>
        <div>
            <div id="r-result" style="width:20%;float:left"></div>
            <div style="width:79.9%;float:right;" id="map"></div>
        </div>
</body>
</html>
            <script type="text/javascript">
                var markerArr = [
                    { title: "名称:预设1", point: "113.264531,23.157003", address: "预设1", tel: "0" },
                    { title: "名称:预设2", point: "113.330934,23.113401", address: "预设2 ", tel: "0" }

                ];

                function map_init(e) {
                    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); //跳动的动画
                        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(){
                                    //map.centerAndZoom(point[k], 18);
                                    //marker[k].openInfoWindow(info[k]);
                                    searchInfoWindow[k].open(marker[k]);
                                }
                            })(i)                            
                        ); 
                    }                  
                    
                    var local = new BMap.LocalSearch(map, {
                        renderOptions: {map: map, panel: "r-result"}
                    });
                    local.search(e);
                }
                //异步调用百度js
                function map_load() {
                    var load = document.createElement("script");
                    load.src = "http://api.map.baidu.com/api?v=2.0&ak=密钥&callback=map_init";
                    document.body.appendChild(load);
                }
                //按输入的内容进行搜索
                function searchMap(){
                     var content=document.getElementById("txtContent").value;
                     map_init(content);
                }
                //键盘回车事件
                function EnterPress(){ 
                    if(event.keyCode == 13){ 
                        searchMap(); //调用输入框搜索事件
                    } 
                } 
                window.onload = map_load;
            </script>


  • 全部评论(0)
资讯详情页最新发布上方横幅
最新发布的资讯信息
【综合技术文档|其他】武汉肺炎最新疫情,2020全国新型冠状病毒肺炎实时分布图(2020-01-29 16:24)
【源码动态|本网专稿】马云预言正确!扫码支付即将被淘汰,三种支付方式悄然兴起(2019-12-28 23:55)
【源码动态|本网专稿】庆余年第二季即将开拍 陈道明、吴刚、肖战缺席,女演员不变(2019-12-28 23:46)
【棋牌技术|】2020年棋牌游戏APP行业发展动向(2019-11-20 18:17)
【源码动态|本网专稿】取代微信的下一代产品是什么?(2019-01-30 10:59)
【棋牌技术|】房卡麻将系列之红中麻将架设技术文章教程(2018-06-20 00:23)
【棋牌技术|】fatal error C1083 无法打开包括文件解决方法(2018-06-20 00:23)
【棋牌技术|】网狐房卡麻将源码修改默认选择游戏玩法(2018-06-20 00:22)
【棋牌技术|】大富豪游戏配置文件名详细讲解(2018-06-20 00:21)
【棋牌技术|】大富豪数据库详细说明键值表(2018-06-20 00:19)