谷歌地图api v3 - 使用图块

时间:2016-01-08 11:16:12

标签: google-maps-api-3

我目前正致力于将区域划分为相等的部分,并使用瓷砖或矩形将这些部分显示给地图上的用户,无论哪个看起来不错。我知道该地区的经纬度。我使用以下代码。下面给出了几个要求。 (1)使用此代码,我无法将区域划分为完全相等的部分。 (2)用瓷砖或矩形划分后,如何得到每个瓷砖的中心。

任何帮助都将不胜感激。

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: northEast.lat() + (tileHeight * y),
            south: southWest.lat(),
            east: northEast.lng(),
            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

          });
        }
      }
    }   
  }   

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

1 个答案:

答案 0 :(得分:3)

在迭代值时增加正方形的边缘并将起点固定为左下角:

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',
        strokeWeight: 2,
        map: map,
        bounds: areaBounds

      });
    }
  }
}   

proof of concept fiddle

screenshot of resulting map

代码段

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

        });
      }
    }
  }
}

google.maps.event.addDomListener(window, "load", initialize);
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>