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 function and our LatLngUtils.distance function helpful.
Constructor and Description |
---|
AreaDivider(double setNorth,
double setEast,
double setSouth,
double setWest,
double 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 |
getXCoordinate(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 |
getYCoordinate(com.google.android.gms.maps.model.LatLng location)
Gets the Y coordinate of the cell containing the specified location.
|
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, double setCellSize)
setNorth
- latitude of the north boundarysetEast
- longitude of the east boundarysetSouth
- latitude of the south boundarysetWest
- longitude of the west boundarysetCellSize
- the requested side length of each cell, in meterspublic com.google.android.gms.maps.model.LatLngBounds getCellBounds(int x, int y)
x
- the cell's X coordinatey
- the cell's Y coordinatepublic int getXCells()
public int getXCoordinate(com.google.android.gms.maps.model.LatLng location)
The point is not necessarily within the area. If it is not, the return value is up to you. However, you will want some way to tell from GameActivity that the location is not in a valid cell. Allowing the returned coordinate to be negative or >= getXCells() is fine, as is always returning a sentinel value like -1 for an out-of-bounds location.
location
- the locationpublic int getYCells()
public int getYCoordinate(com.google.android.gms.maps.model.LatLng location)
The point is not necessarily within the area. If it is not, the return value is up to you. However, you will want some way to tell from GameActivity that the location is not in a valid cell. Allowing the returned coordinate to be negative or >= getYCells() is fine, as is always returning a sentinel value like -1 for an out-of-bounds location.
location
- the locationpublic 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, you do not need the extra line to make the line appear to have a border.
map
- the Google map to draw on