public class AreaDivider
extends java.lang.Object
Each cell is given an X and Y coordinate. X increases from the west boundary toward the east boundary; Y increases from south to north. So (0, 0) is the cell in the southwest corner. (0, 1) is the cell just north of the southwestern corner cell.
Instances of this class are created with a desired cell size. However, it is unlikely that the area dimensions will be an exact multiple of that length, so placing fully sized cells would leave a small "sliver" on the east or north side. Length should be redistributed so that each cell is exactly the same size. If the area is 70 meters long in one dimension and the cell size is 20 meters, there will be four cells in that dimension (there's room for three full cells plus a 10m sliver), each of which is 70 / 4 = 17.5 meters long. Redistribution happens independently for the two dimensions, so a 70x40 area would be divided into 17.5x20.0 cells with a 20m cell size.
You may find Java's Math.ceil and Math.floor functions and our LatLngUtils.distance function helpful.
Constructor and Description |
---|
AreaDivider(double setNorth,
double setEast,
double setSouth,
double setWest,
int setCellSize)
Creates an AreaDivider for an area.
|
Modifier and Type | Method and Description |
---|---|
com.google.android.gms.maps.model.LatLngBounds |
getCellBounds(int x,
int y)
Gets the boundaries of the specified cell as a Google Maps LatLngBounds object.
|
int |
getXCells()
Gets the number of cells between the west and east boundaries.
|
int |
getXIndex(com.google.android.gms.maps.model.LatLng location)
Gets the X coordinate of the cell containing the specified location.
|
int |
getYCells()
Gets the number of cells between the south and north boundaries.
|
int |
getYIndex(com.google.android.gms.maps.model.LatLng location)
Gets the Y coordinate of the cell containing the specified location.
|
boolean |
isValid()
Returns whether the configuration provided to the constructor is valid.
|
void |
renderGrid(com.google.android.gms.maps.GoogleMap map)
Draws the grid to a map using solid black polylines.
|
public AreaDivider(double setNorth, double setEast, double setSouth, double setWest, int setCellSize)
Note the order of parameters carefully. A mismatch in interpretation of the arguments
between the constructor and its callers will result in unreasonable dimensions.
The isValid
function can detect some such problems.
setNorth
- latitude of the north boundarysetEast
- longitude of the east boundarysetSouth
- latitude of the south boundarysetWest
- longitude of the east boundarysetCellSize
- the requested side length of each cell, in meterspublic com.google.android.gms.maps.model.LatLngBounds getCellBounds(int x, int y)
Note that the LatLngBounds constructor takes the southwest and northeast points of the rectangular region as LatLng objects.
x
- the cell's X coordinatey
- the cell's Y coordinatepublic int getXCells()
public int getXIndex(com.google.android.gms.maps.model.LatLng location)
The point is not necessarily within the area. If it is not, the return value must not appear to be a valid cell index. For example, returning 0 for a point even slightly west of the west boundary is not allowed.
location
- the locationpublic int getYCells()
public int getYIndex(com.google.android.gms.maps.model.LatLng location)
The point is not necessarily within the area. If it is not, the return value must not appear to be a valid cell index. For example, returning 0 for a point even slightly south of the south boundary is not allowed.
location
- the locationpublic boolean isValid()
The configuration is valid if the cell size is positive the bounds delimit a region of positive area. That is, the east boundary must be strictly further east than the west boundary and the north boundary must be strictly further north than the south boundary.
Due to floating-point strangeness, you may find our LatLngUtils.same function helpful if equality comparison of double variables does not work as expected.
public void renderGrid(com.google.android.gms.maps.GoogleMap map)
There should be one line on each of the four boundaries of the overall area and as many internal lines as necessary to divide the rows and columns of the grid. Each line should span the whole width or height of the area rather than the side of just one cell. For example, an area divided into a 2x3 grid would be drawn with 7 lines total: 4 for the outer boundaries, 1 vertical line to divide the west half from the east half (2 columns), and 2 horizontal lines to divide the area into 3 rows.
See the provided addLine function from GameActivity for how to add a line to the map. Since these lines should be black, they should not be paired with any extra "border" lines.
If equality comparisons of double variables do not work as expected, consider taking advantage of our LatLngUtils.same function.
map
- the Google map to draw on