1. 程式人生 > >自定義百度地圖上顯示的覆蓋物

自定義百度地圖上顯示的覆蓋物

<!DOCTYPE html>  
<html>  
<head>  
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>Hello, World</title>  
<style type="text/css">  
html{height:100%}  
body{height:100%;margin:0px;padding:0px}  
#container{height:100%}  
</style>  
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=6241064f1bb6ffb432dff3aae77c1d09"></script>
</head>  

<body>  
<div id="container"></div> 
<div id="log"></div> 
<script type="text/javascript"> 
var map = new BMap.Map("container");          // 建立地圖例項  
var point = new BMap.Point(116.404, 39.915);  // 建立點座標  
map.centerAndZoom(point, 6);                 // 初始化地圖,設定中心點座標和地圖級別  
map.enableScrollWheelZoom();
map.addControl(new BMap.NavigationControl());
map.addControl(new BMap.NavigationControl());    
map.addControl(new BMap.ScaleControl());    
map.addControl(new BMap.OverviewMapControl());    
map.addControl(new BMap.MapTypeControl());


map.addEventListener("zoomend", function(){    
 var zoom = this.getZoom();
 if(zoom > 13){
     dislayobj('obj1','none');
     dislayobj('obj2','block');
 }else{
     dislayobj('obj2','none');
     dislayobj('obj1','block');
 }    
});

function dislayobj (class_name, status) {
    var displayObj = document.getElementsByClassName(class_name);
     for (var i = 0; i < displayObj.length; i++) {
         displayObj[i].style.display = status;
     };
}

// 複雜的自定義覆蓋物
function ComplexCustomOverlay(point, text, num, class_name, is_display){
  this._point = point;
  this._text = text;
  this._num = num;
  this._class = class_name;
  this._is_display = is_display;
}
ComplexCustomOverlay.prototype = new BMap.Overlay();
ComplexCustomOverlay.prototype.initialize = function(map){

    this._map = map;
    var div = this._div = document.createElement("div");
    div.style.position = "absolute";
    div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
    div.style.background = "rgba(13, 173, 81, 0.9)";
    div.style.border = "1px solid green";
    div.style.color = "white";
    div.style.borderRadius = "50%";
    div.style.height = "100px";
    div.style.width = "100px";
    div.style.textAlign = "center";
    div.style.padding = "2px";
    div.style.whiteSpace = "nowrap";
    div.style.MozUserSelect = "none";
    div.style.fontSize = "14px";
    if(this._is_display){
        div.style.display = 'block';
    }else{
        div.style.display = 'none';
    }
    div.setAttribute('class',this._class);
    var p1 = this._span = document.createElement("p");
    p1.style.paddingTop = "14px";
    div.appendChild(p1);
    p1.appendChild(document.createTextNode(this._text));    
    var p2 = this._span = document.createElement("p");
    div.appendChild(p2);
    p2.style.fontSize = '16px';
    p2.appendChild(document.createTextNode(this._num));      
    var that = this;

    var arrow = this._arrow = document.createElement("div");
    arrow.style.position = "absolute";
    arrow.style.width = "11px";
    arrow.style.height = "10px";
    arrow.style.top = "22px";
    arrow.style.left = "10px";
    arrow.style.overflow = "hidden";
    div.appendChild(arrow);

    div.onmouseover = function(){
        div.style.background = "rgba(238, 93, 91, 0.9)";
    this.style.borderColor = "#EE5D5B";
    }

    div.onmouseout = function(){
      div.style.background = "rgba(13, 173, 81, 0.9)";
      div.style.borderColor = "green";
    }

    map.getPanes().labelPane.appendChild(div);

  return div;
}
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";
}



var txt = "昌平實驗基地", num = parseInt(Math.random() * 1000,10) + "套" ;
var txt1 = "天津空港", num1 = parseInt(Math.random() * 1000,10) + "套" ;
var txt2 = "北京機殼儀器中心", num2 = parseInt(Math.random() * 1000,10) + "套" ;
var txt3 = "磁器口科研基地", num3 = parseInt(Math.random() * 1000,10) + "套" ;

var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(116.407845,39.914101), txt, num, 'obj1', 1);
var myCompOverlay1 = new ComplexCustomOverlay(new BMap.Point(116.445087,39.956536), txt1, num1, 'obj1', 1);
var myCompOverlay2 = new ComplexCustomOverlay(new BMap.Point(116.360719,39.947963), txt2, num2, 'obj2', 0);
var myCompOverlay3 = new ComplexCustomOverlay(new BMap.Point(116.425298,39.899595), txt3, num3, 'obj2', 0);

map.addOverlay(myCompOverlay);
map.addOverlay(myCompOverlay1);
map.addOverlay(myCompOverlay2);
map.addOverlay(myCompOverlay3);
</script>  
</body>  
</html>