百度地图API,区域内搜索

3 篇文章 0 订阅
1 篇文章 0 订阅
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>区域内搜索 </title>
    <style type="text/css">
        body, html
        {
            width: 100%;
            height: 100%;
            overflow: hidden;
            margin: 0;
        }
        #l-map
        {
            height: 100%;
            width: 75%;
            float: left;
            border-right: 2px solid #bcbcbc;
        }
        #r-result
        {
            height: 100%;
            width: 24%;
            float: left;
            overflow: auto;
        }
    </style>
    <script src="/js/jquery.js" type="text/javascript"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=abcd"></script>
</head>
<body>
    <div id="l-map">
    </div>
    <div id="r-result">
    </div>
</body>
</html>
<script type="text/javascript">
//地图
    var map = new BMap.Map("l-map");
    var mPoint = new BMap.Point(116.404, 39.915);  
    $.ajax({
     type:"post",
  url:"*.aspx?action=getdata",
  success:function(msg){
   mPoint = new BMap.Point(msg.split(',')[0],msg.split(',')[1]);
    }
    });
setTimeout(function(){
    map.enableScrollWheelZoom();
    map.centerAndZoom(mPoint,15);
    var circle = new BMap.Circle(mPoint,1000,{fillColor:"blue", strokeWeight: 1 ,fillOpacity: 0.3, strokeOpacity: 0.3});
    map.addOverlay(circle); 
    var options = { onSearchComplete: function(results){ if (local.getStatus() == BMAP_STATUS_SUCCESS){
 var s=[]; 
 for (var i = 0; i < results.getCurrentNumPois(); i ++)
 {
 s.push((i+1)+": "+results.getPoi(i).title + ", " + results.getPoi(i).address+"</br>");
 } 
 document.getElementById("r-result").innerHTML = s.join("");
  } } };
    //1.将结果显示右边的标签中
     var local = new BMap.LocalSearch(map, options); 
     //2.将结果显示在地图的区域内
    // var local =  new BMap.LocalSearch(map, {renderOptions: {map: map, autoViewport: false}});  
    var bounds = getSquareBounds(circle.getCenter(),circle.getRadius());
   local.searchInBounds("公交车站",bounds); 
},1000);
     
    function getSquareBounds(centerPoi,r){
        var a = Math.sqrt(2) * r; //正方形边长
        mPoi = getMecator(centerPoi);
        var x0 = mPoi.x, y0 = mPoi.y;
        var x1 = x0 + a / 2 , y1 = y0 + a / 2;//东北点
        var x2 = x0 - a / 2 , y2 = y0 - a / 2;//西南点
         var ne = getPoi(new BMap.Pixel(x1, y1)), sw = getPoi(new BMap.Pixel(x2, y2));
        return new BMap.Bounds(sw, ne);        
        
    }

    function getMecator(poi){
        return map.getMapType().getProjection().lngLatToPoint(poi);
    }

    function getPoi(mecator){
        return map.getMapType().getProjection().pointToLngLat(mecator);
    }
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值