且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我怎样才能将Google地图显示分成相同数量的部分?

更新时间:2022-10-15 16:59:05

我假设你在150个较小瓷砖的区域坐标之后?



map.getBounds()返回一个GLatLngBounds对象。这包括getSouthWest()和getNorthEast()方法。因此,从可见地图的左下角和右上角的纬度/经度值可以推导出150个较小区域的坐标:


  • 将宽度(getNorthEast()的经度 - getSouthWest()的经度)除以15,以确定宽度。 分隔高度(getNorthEast()的纬度 - getSouthWest()的纬度除以10,以确定瓷砖高度。
  • 根据x和y的偏移量乘以宽度和高度,计算特定单元格的坐标。
  • >


因此,要为特定单元索引(x,y)实例化GLatLngBounds(sw?:GLatLng,ne?:GLatLng):

  // bounds是map.getBounds()的结果
northLat = bounds.getNorthEast()。lat()+(tileHeight * y);
westLng = bounds.getSouthWest()。lng()+(tileWidth * x);
southLat = northLat + tileHeight;
eastLng = westLng + tileHeight;

tileBounds = new GLatLngBounds(new GLatLng(southLat,westLng),new GLatLng(northLat,eastLng));


I want to divide the map display area into a number of equal parts. For example: 10 parts horizontally and 15 parts vertically, resulting in 150 equal parts.

I do not know if Google Maps support such a thing...

map.getBounds() return the total visible area; so ,

  var bounds = map.getBounds();
alert("main: "+bounds.isEmpty());
  var southWest = bounds.getSouthWest();
  var northEast = bounds.getNorthEast();
  var tileWidth  = (northEast.lng() - southWest.lng()) / 10;
  var tileHeight = (northEast.lat() - southWest.lat()) / 2;

  for (var x=0; x < 10 ; x++)
{
  for (var y=0; y < 2 ; y++)
   {
    var northLat = bounds.getNorthEast().lat () + (tileHeight * y);
    var westLng = bounds.getSouthWest().lng () + (tileWidth * x);
    var southLat = northLat + tileHeight;
    var eastLng = westLng + tileHeight;

            var tileBounds = new GLatLngBounds(new GLatLng(southLat, westLng), new GLatLng(northLat, eastLng));
    alert(tileBounds.isEmpty());

     }
 }

now the problem is tileBounds.isEmpty() returns TRUE !!

I cant able to find where I am missing something !! Any Help !!

I am assuming you are after the region coordinates for the 150 smaller tiles?

map.getBounds () returns a GLatLngBounds object. This includes getSouthWest () and getNorthEast () methods. So from the latitude/longitude values for the bottom left and top right hand corners of the visible map you could derive the coordinates of the set of 150 smaller regions:

  • Divide the width (longitude of getNorthEast () - longitude of getSouthWest ()) by 15, to determine tile width.
  • Divide the height (latitude of getNorthEast () - latitude of getSouthWest ()) by 10, to determine tile height.
  • calculate the coordinates for a particular cell based on it's offset in x and y multiplied by the width and height.

So to instantiate a GLatLngBounds(sw?:GLatLng, ne?:GLatLng) for a particular cell index (x, y):

// bounds is the map.getBounds () result
northLat = bounds.getNorthEast().lat () + (tileHeight * y);
westLng = bounds.getSouthWest().lng () + (tileWidth * x);
southLat = northLat + tileHeight;
eastLng = westLng + tileHeight;

tileBounds = new GLatLngBounds (new GLatLng (southLat, westLng), new GLatLng (northLat, eastLng));