baidu地图api 多个标签 在同一视野内显示 自动配置合适的缩放等级

setViewport  函数 
//初始化
var myMap = new BMap.Map("container");
myMap.addControl(new BMap.NavigationControl());
myMap.enableScrollWheelZoom();    //启用滚轮放大缩小,默认禁用
myMap.enableContinuousZoom(); 
 
    var gspinfo = <?php  echo json_encode($map_info);?>; //将php数组通过json方式传给js
    
    //可以转化gps坐标
 
    myMap.centerAndZoom(new BMap.Point(lat_point, lng_point), leva); 
    var mapWforGPS = new BMapLib.MapWrapper(myMap, BMapLib.COORD_TYPE_GPS);  
    //================================
function ComplexCustomOverlay(point, text, mouseoverText){
      this._point = point;
      this._text = text;
      this._overText = mouseoverText;
    }
    ComplexCustomOverlay.prototype = new BMap.Overlay();
    ComplexCustomOverlay.prototype.initialize = function(map){
      this._map = map;
      var divm = this._div = document.createElement("div");
      divm.style.position = "absolute";
      divm.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
      divm.style.backgroundColor = "#EE5D5B";
      divm.style.border = "1px solid #BC3B3A";
      divm.style.color = "white";
      divm.style.height = "18px";
      divm.style.padding = "2px";
      divm.style.lineHeight = "18px";
      divm.style.whiteSpace = "nowrap";
      divm.style.MozUserSelect = "none";
      divm.style.fontSize = "12px"
      var span = this._span = document.createElement("span");
      divm.appendChild(span);
      span.appendChild(document.createTextNode(this._text));      
      var that = this;
      var arrow = this._arrow = document.createElement("div");
      arrow.style.background = "url(http://map.baidu.com/fwmap/upload/r/map/fwmap/static/house/images/label.png) no-repeat";
      arrow.style.position = "absolute";
      arrow.style.width = "11px";
      arrow.style.height = "10px";
      arrow.style.top = "22px";
      arrow.style.left = "10px";
      arrow.style.overflow = "hidden";
      divm.appendChild(arrow);
     
      divm.onmouseover = function(){
        this.style.backgroundColor = "#6BADCA";
        this.style.borderColor = "#0000ff";
        this.getElementsByTagName("span")[0].innerHTML = that._overText;
        arrow.style.backgroundPosition = "0px -20px";
      }
      divm.onmouseout = function(){
        this.style.backgroundColor = "#EE5D5B";
        this.style.borderColor = "#BC3B3A";
        this.getElementsByTagName("span")[0].innerHTML = that._text;
        arrow.style.backgroundPosition = "0px 0px";
      }
      myMap.getPanes().labelPane.appendChild(divm);
      
      return divm;
    }
    ComplexCustomOverlay.prototype.draw = function(){
      var map = this._map;
      var pixel = map.pointToOverlayPixel(this._point);
      this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
      this._div.style.top  = pixel.y - 30 + "px";
    }
    //==============================
      
       points = Array();
    for (var i = 0; i < gl_; i++) {
        //添加gps坐标mkr
        //var gpsMkr = new BMap.Marker(new BMap.Point(/*GPS坐标*/gspinfo[i].lat ,gspinfo[i].lng));
        
        //myMap.centerAndZoom(gpsMkr, leva); 
        
        //mapWforGPS.addOverlay(gpsMkr);  
        // var marker = new BMap.Marker(gpsMkr); 
        //var labelgps = new BMap.Label(gspinfo[i].drugname,{offset:new BMap.Size(20,-10)});
        
        //gpsMkr.setLabel(labelgps);
          points[i]= new BMap.Point(gspinfo[i].lat,gspinfo[i].lng);
        //===========================
          var txt = gspinfo[i].drugname, mouseoverTxt =gspinfo[i].asi+' '+txt;
            //添加自定义标签              
          var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(gspinfo[i].lat,gspinfo[i].lng)," "+gspinfo[i].asi+" ",mouseoverTxt);
          myMap.addOverlay(myCompOverlay);
          //points[$i]=new BMap.Point(gspinfo[i].lat,gspinfo[i].lng);
        } 
        myMap.setViewport(points); //调用setViewport 设置视野
//points = [new BMap.Point(gspinfo[i].lat,gspinfo[i].lng),new BMap.Point(gspinfo[i].lat,gspinfo[i].lng),new BMap.Point(gspinfo[i].lat,gspinfo[i].lng) ]
/


转载于:https://my.oschina.net/startphp/blog/192904

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值