Google地图 - 将地区划分为相等的部分,并在地图上仅显示点击的地区

时间:2016-02-19 07:24:39

标签: google-maps google-maps-api-3

以下是将地图划分为相等区域的代码示例。单击矩形中心的标记时,如何在Google地图上仅显示该特定矩形区域?单击标记后,所有其他区域不应位于地图中。

function initialize() {     
var myLatlng;    
var mapOptions;

myLatlng = new google.maps.LatLng(29.98439980, -95.34140015);

    mapOptions = {
      zoom: 16,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(
      document.getElementById("map-canvas"), mapOptions);

    google.maps.event.addListenerOnce(map, 'idle', function() {
      drawRectangle(map);
    });

    function drawRectangle(map) {
      var bounds = map.getBounds();
      var southWest = bounds.getSouthWest();
      var northEast = bounds.getNorthEast();

      var numberOfParts = 4;

      var tileWidth = (northEast.lng() - southWest.lng()) / numberOfParts;
      var tileHeight = (northEast.lat() - southWest.lat()) / numberOfParts;
      for (var x = 0; x < numberOfParts; x++) {
        for (var y = 0; y < numberOfParts; y++) {
          var areaBounds = {
            north: southWest.lat() + (tileHeight * (y+1)),
            south: southWest.lat() + (tileHeight * y),
            east: southWest.lng() + (tileWidth * (x+1)),
            west: southWest.lng() + (tileWidth * x)
          };

          var area = new google.maps.Rectangle({
            strokeColor: '#FF0000',
            //strokeOpacity: 0.8,
            strokeWeight: 2,
            //fillColor: '#FF0000',
            //fillOpacity: 0.35,
            map: map,
            bounds: areaBounds
          });
          var centerMark = new google.maps.Marker({
          position: area.getBounds().getCenter(),
          map: map,
          title: area.getBounds().getCenter().toUrlValue(6)
          });
        }
      }
    }   
  }   

google.maps.event.addDomListener(window, "load", initialize); 

我已尝试使用地图边界代码,但它无法按预期工作。它只有在我增加变焦时才有效。但每个地区的缩放可能会有所不同。

var bounds = new google.maps.LatLngBounds();
    bounds.extend(southLatitude, eastLongitude);
    bounds.extend(northLatitude, westLongitude);
    map.fitBounds(bounds);

1 个答案:

答案 0 :(得分:1)

您可以将每个矩形的边界添加为中心标记的属性,然后使用google.maps.event.addListener(centerMark, 'click', function(evt) { // center and zoom the map to completely show the area map.fitBounds(this.area); google.maps.event.addListenerOnce(map, 'bounds_changed', function() { // increase the zoom by one level to remove the padding. map.setZoom(map.getZoom()+1); }) }); 将地图居中并缩放到矩形区域:

function initialize() {
  var myLatlng;
  var mapOptions;

  myLatlng = new google.maps.LatLng(29.98439980, -95.34140015);

  mapOptions = {
    zoom: 10,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  var map = new google.maps.Map(
    document.getElementById("map-canvas"), mapOptions);

  google.maps.event.addListenerOnce(map, 'idle', function() {
    drawRectangle(map);
  });

  function drawRectangle(map) {
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();

    var numberOfParts = 4;

    var tileWidth = (northEast.lng() - southWest.lng()) / numberOfParts;
    var tileHeight = (northEast.lat() - southWest.lat()) / numberOfParts;
    for (var x = 0; x < numberOfParts; x++) {
      for (var y = 0; y < numberOfParts; y++) {
        var areaBounds = {
          north: southWest.lat() + (tileHeight * (y + 1)),
          south: southWest.lat() + (tileHeight * y),
          east: southWest.lng() + (tileWidth * (x + 1)),
          west: southWest.lng() + (tileWidth * x)
        };

        var area = new google.maps.Rectangle({
          strokeColor: '#FF0000',
          //strokeOpacity: 0.8,
          strokeWeight: 2,
          //fillColor: '#FF0000',
          //fillOpacity: 0.35,
          map: map,
          bounds: areaBounds
        });
        var centerMark = new google.maps.Marker({
          position: area.getBounds().getCenter(),
          map: map,
          area: areaBounds,
          title: area.getBounds().getCenter().toUrlValue(6)
        });
        google.maps.event.addListener(centerMark, 'click', function(evt) {
          map.fitBounds(this.area);
          google.maps.event.addListenerOnce(map, 'bounds_changed', function() {
            map.setZoom(map.getZoom() + 1);
          });

        });
      }
    }
  }
}

google.maps.event.addDomListener(window, "load", initialize);

authenticating Azure Resource Manager request

代码段

html,
body,
#map-canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>
getcol