点击谷歌地图API上的按钮,即可显示可拖动的矩形

时间:2015-08-30 18:20:50

标签: javascript google-maps

我正在玩谷歌地图api,我试图在用户点击按钮时弹出矩形。我正在研究使用API​​的示例,我想出了这个JSFiddle。但是,当单击按钮时,我无法显示矩形。我希望它们在单击选择区域按钮时出现在用户屏幕的中心。

这是我拼凑不好的地方:

      #map {
        width: 500px;
        height: 500px;
        center: true;
      }
<!DOCTYPE html>
<html>
  <head>
    <title>User-editable Shapes</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
 
  </head>
  <body>
    <div id="map"></div>
    <script>
var map;
var rectangle;
var infoWindow;
var selectRegion = null;
var penang = {lat: 5.466277, lng: 100.289981};

/**
 * The CenterControl adds a control to the map that recenters the map on Chicago.
 * This constructor takes the control DIV as an argument.
 * @constructor
 */
function CenterControl(controlDiv, map) {

  // Set CSS for the control border.
  var controlUI = document.createElement('div');
  controlUI.style.backgroundColor = '#fff';
  controlUI.style.border = '2px solid #fff';
  controlUI.style.borderRadius = '3px';
  controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
  controlUI.style.cursor = 'pointer';
  controlUI.style.marginBottom = '22px';
  controlUI.style.textAlign = 'center';
  controlUI.title = 'Click to recenter the map';
  controlDiv.appendChild(controlUI);

  // Set CSS for the control interior.
  var controlText = document.createElement('div');
  controlText.style.color = 'rgb(25,25,25)';
  controlText.style.fontFamily = 'Roboto,Arial,sans-serif';
  controlText.style.fontSize = '16px';
  controlText.style.lineHeight = '38px';
  controlText.style.paddingLeft = '5px';
  controlText.style.paddingRight = '5px';
  controlText.innerHTML = 'Select Region';
  controlUI.appendChild(controlText);

  // Setup the click event listeners: simply set the map to Chicago.
  controlUI.addEventListener('click', function() {
    centerView = map.getCenter();
    var bounds = new google.maps.LatLngBounds(
      new google.maps.LatLng(centerView.lat() - 0.1, centerView() + 0.1),
      new google.maps.LatLng(centerView.lng() + 0.1, centerView() - 0.1)
      );
  // Define the rectangle and set its editable property to true.
    rectangle = new google.maps.Rectangle({
      bounds: bounds,
      editable: true,
      draggable: true
      });
    
    selectRegion = 'map';    
 
        
  });

}

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 15,
    center: penang
  });

  // Create the DIV to hold the control and call the CenterControl() constructor
  // passing in this DIV.
  var centerControlDiv = document.createElement('div');
  var centerControl = new CenterControl(centerControlDiv, map);

  centerControlDiv.index = 1;
  map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv);

rectangle.setMap(selectRegion);
        
        
}


    </script>
    <script src="https://maps.googleapis.com/maps/api/js?&callback=initMap&signed_in=true" async defer>
    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

我尝试过你的代码。我发现了两个问题

中的错误
  new google.maps.LatLng(centerView.lat() - 0.1, centerView() + 0.1),
  new google.maps.LatLng(centerView.lng() + 0.1, centerView() - 0.1)

这是正确的

      new google.maps.LatLng(centerView.lat() - 0.001, centerView.lng() - 0.001),
      new google.maps.LatLng(centerView.lat()  + 0.001, centerView.lng() + 0.001)

将矩形设为不可见并分配给地图

我改变了这种方式

rectangle = new google.maps.Rectangle({
  bounds: bounds,
  editable: true,
  draggable: true,
  strokeColor: '#FF0000',
  strokeOpacity: 0.8,
  strokeWeight: 2,
  fillColor: '#FF0000',
  fillOpacity: 0.35,
  map: map,
  });

这是完整的工作代码

<body>
    <div id="map"></div>
    <script>
var map;
var rectangle;
var infoWindow;
var selectRegion = null;
var penang = {lat: 5.466277, lng: 100.289981};

/**
 * The CenterControl adds a control to the map that recenters the map on Chicago.
 * This constructor takes the control DIV as an argument.
 * @constructor
 */
function CenterControl(controlDiv, map) {

  // Set CSS for the control border.

  var controlUI = document.createElement('div');
  controlUI.style.backgroundColor = '#fff';
  controlUI.style.border = '2px solid #fff';
  controlUI.style.borderRadius = '3px';
  controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
  controlUI.style.cursor = 'pointer';
  controlUI.style.marginBottom = '22px';
  controlUI.style.textAlign = 'center';
  controlUI.title = 'Click to recenter the map';
  controlDiv.appendChild(controlUI);

  // Set CSS for the control interior.
  var controlText = document.createElement('div');
  controlText.style.color = 'rgb(25,25,25)';
  controlText.style.fontFamily = 'Roboto,Arial,sans-serif';
  controlText.style.fontSize = '16px';
  controlText.style.lineHeight = '38px';
  controlText.style.paddingLeft = '5px';
  controlText.style.paddingRight = '5px';
  controlText.innerHTML = 'Select Region';
  controlUI.appendChild(controlText);

  // Setup the click event listeners: simply set the map to Chicago.
  controlUI.addEventListener('click', function() {

    centerView = map.getCenter();
    var bounds = new google.maps.LatLngBounds(
      new google.maps.LatLng(centerView.lat() - 0.001, centerView.lng() - 0.001),
      new google.maps.LatLng(centerView.lat()  + 0.001, centerView.lng() + 0.001)
      );
  // Define the rectangle and set its editable property to true.
    rectangle = new google.maps.Rectangle({
      bounds: bounds,
      editable: true,
      draggable: true,
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 2,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      map: map,
      });


    selectRegion = 'map';    


  });

}

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 15,
    center: penang
  });

  // Create the DIV to hold the control and call the CenterControl() constructor
  // passing in this DIV.
  var centerControlDiv = document.createElement('div');
  var centerControl = new CenterControl(centerControlDiv, map);

  centerControlDiv.index = 1;
  map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv);

   //rectangle.setMap(selectRegion);


}


    </script>
    <script src="https://maps.googleapis.com/maps/api/js?&callback=initMap&signed_in=true" async defer>
    </script>
  </body>