Baidu Map API: Custom Controls

HTML:

<!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 {
      margin: 0px;
      padding: 0px;
      height: 100%;
    }

    #container {
      margin: 100px auto;
      width: 1300px;
      height: 700px;
    }
  </style>
</head>

<body>
<div id="container"></div>
</body>
</html>

JS:

< script type = "text/javascript" > 
  var map =  new BMap.Map( " container " );
   // Create a map instance 
  var point =  new BMap.Point( 118.024381 , 36.812327 );
   // Create point coordinates 
  map.centerAndZoom( point, 15 );
   // Initialize the map, set the center point coordinates and map level 
  map.enableScrollWheelZoom( true );

  //自定义控件
  function ZoomControl() {
    this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
    this.defaultOffest = new BMap.Control();
  }

  // Inherited from BMap.Control via JavaScript's prototype property 
  ZoomControl.prototype =  new BMap.Control();
  ZoomControl.prototype.initialize = function (map) {
    var div = document.createElement('div');
    div.appendChild(document.createTextNode( " Zoom level 2 " ));
    div.style.cursor = "pointer";
    div.style.border = "1px solid gray";
    div.style.backgroundColor = "white";
    div.onclick = function () {
      map.zoomTo(map.getZoom() + 2);
    };
    // Add DOM elements to the map 
    map.getContainer().appendChild(div);
     // Return DOM elements 
    return div;
  }
  // Create a control instance 
  var myZoomCtrl =  new ZoomControl();
   // Add to the map 
  map.addControl(myZoomCtrl);
 </ script >

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325022641&siteId=291194637