百度地图Api进阶教程-创建标注和自定义标注3.html

来源:互联网 发布:淘宝买家退款率怎么查 编辑:程序博客网 时间:2024/05/29 09:13

转载:http://blog.csdn.net/sd0902/article/details/8471322


[html] view plaincopy
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>百度地图</title>  
  6.   
  7.   
  8. <script type="text/javascript">  
  9.   
  10.     function initialize() {  
  11.         //---------------------------------------------基础示例---------------------------------------------  
  12.         var map = new BMap.Map("allmap",{minZoom:12,maxZoom:20});            // 创建Map实例  
  13.         //map.centerAndZoom(new BMap.Point(116.4035,39.915),15);  //初始化时,即可设置中心点和地图缩放级别。  
  14.         map.centerAndZoom("成都",13);                     // 初始化地图,设置中心点坐标和地图级别。  
  15.         map.enableScrollWheelZoom(true);//鼠标滑动轮子可以滚动  
  16.           
  17.         map.addEventListener("click", function(e){  
  18.             document.getElementById("r-result").innerHTML = e.point.lng + ", " + e.point.lat;  
  19.         });  
  20.         //---------------------------------------------遮盖物---------------------------------------------  
  21.          var point = new BMap.Point(104.117287, 30.685906);//默认  
  22.          // 创建标注对象并添加到地图    
  23.          var marker = new BMap.Marker(point);    
  24.          map.addOverlay(marker);    
  25.   
  26.            
  27.         var point = new BMap.Point(104.057287, 30.685906);//图片  
  28.         var myIcon = new BMap.Icon("http://t3.baidu.com/it/u=1119318591,884730191&fm=0&gp=0.jpg", new BMap.Size(55, 55));      
  29.          // 创建标注对象并添加到地图    
  30.          var marker = new BMap.Marker(point, {icon: myIcon});    
  31.          map.addOverlay(marker);    
  32.   
  33.          //自定义遮盖物  
  34.         // 定义自定义覆盖物的构造函数    
  35.          var point = new BMap.Point(104.117287, 30.695906);//自定义遮盖物  
  36.         function SquareOverlay(point, length, color){    
  37.             this._point = point;    
  38.             this._length = length;    
  39.             this._color = color;    
  40.         }    
  41.         // 继承API的BMap.Overlay    
  42.         SquareOverlay.prototype = new BMap.Overlay();    
  43.   
  44.         // 实现初始化方法    
  45.         SquareOverlay.prototype.initialize = function(map){    
  46.             // 保存map对象实例    
  47.              this._map = map;        
  48.              // 创建div元素,作为自定义覆盖物的容器    
  49.              var div = document.createElement("div");    
  50.              div.style.position = "absolute";        
  51.              // 可以根据参数设置元素外观    
  52.              div.style.width = this._length + "px";    
  53.              div.style.height = this._length + "px";    
  54.              div.style.background = this._color;      
  55.              // 将div添加到覆盖物容器中    
  56.              map.getPanes().markerPane.appendChild(div);      
  57.              // 保存div实例   
  58.              this._div = div;      
  59.              // 需要将div元素作为方法的返回值,当调用该覆盖物的show、    
  60.              // hide方法,或者对覆盖物进行移除时,API都将操作此元素。    
  61.              return div;    
  62.         }  
  63.         // 实现绘制方法  (您需要在draw方法中设置覆盖物的位置,每当地图状态发生变化(比如:位置移动、级别变化)时,API都会调用覆盖物的draw方法,用于重新计算覆盖物的位置)  
  64.         SquareOverlay.prototype.draw = function(){    
  65.   
  66.               var position = map.pointToOverlayPixel(this._point);  
  67.               this._div.style.left = position.x - this._length / 2 + "px";    
  68.               this._div.style.top = position.y - this._length / 2 + "px";    
  69.         }  
  70.         // 实现显示方法    
  71.         SquareOverlay.prototype.show = function(){    
  72.              if (this._div){    
  73.                 this._div.style.display = "";    
  74.              }    
  75.         }      
  76.         // 实现隐藏方法    
  77.         SquareOverlay.prototype.hide = function(){    
  78.              if (this._div){    
  79.                 this._div.style.display = "none";    
  80.              }    
  81.         }  
  82.         // 添加自定义覆盖物    
  83.         var mySquare = new SquareOverlay(point, 22, "red");    
  84.         map.addOverlay(mySquare);  
  85.     }  
  86.    
  87.     function loadScript() {  
  88.        var script = document.createElement("script");  
  89.        script.src = "http://api.map.baidu.com/api?v=1.4&callback=initialize";  
  90.        document.body.appendChild(script);  
  91.     }  
  92.    
  93.     window.onload = loadScript;  
  94. </script>  
  95.   
  96.   
  97. </head>  
  98.  <body>  
  99.     <div id="r-result" style="float:left;width:100px;">打印坐标</div>  
  100.     <div id="allmap" style="width: 800px; height: 500px"></div>  
  101. </body>  
  102. </html>  
  103.   
  104. <script type="text/javascript">  
  105.     // 移动到某点 map.panTo(new BMap.Point(116.409, 39.918));    
  106.      // map.setZoom(14);   //放到到14级  
  107. </script>