Skip to content

Commit bad61b0

Browse files
committed
Update readme
1 parent 9260197 commit bad61b0

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

README.md

+29-19
Original file line numberDiff line numberDiff line change
@@ -12,41 +12,51 @@ MapGrid is a grid data structure that can be used as an abstraction over the map
1212

1313
You initialize a MapGrid by providing what kind of tiles it will hold and a factory to create these tiles. You also need to give the size of the tile. This applies only at the grid origin which is currently set to be at zero coordinates.
1414

15-
var grid = MapGrid<CustomData>(tileSize: 100000 /* meters */, factory: CustomTileFactory())
15+
var grid = MapGrid<Tile>(tileSize: 100000 /* meters */)
1616

17-
Here is an example of a factory.
17+
And here's an example on how to use it.
1818

1919
```
20-
class CustomTileFactory: TileFactory<CustomTile> {
20+
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
21+
22+
let visibleRegion = mapView.region
23+
let removedTiles = grid.crop(toRegion: visibleRegion)
24+
let newTiles = grid.fill(toRegion: visibleRegion, newTile: self.createTile)
25+
print("update: +\(newTiles.count) -\(removedTiles.count)")
2126
22-
override func value(forMapIndex mapIndex: MapIndex, inMapGrid mapGrid: MapGrid<Tile>) -> CustomTile {
23-
let region = mapGrid.region(at: mapIndex)
24-
/* Here you would provide data, such as annotations for the tile */
25-
return CustomTile(...)
26-
}
27+
mapView.addAnnotations(newTiles.flatMap { $0.item.cities })
28+
mapView.removeAnnotations(removedTiles.flatMap { $0.item.cities })
2729
}
2830
```
2931

30-
Finally you call `update(visibleRegion:)` to get new tiles for the given region. The new tiles you need to add to the map. This will also give you tiles that were removed from the grid and it is up to you to remove the contents of these tiles from the map.
32+
And that's it! With MapGrid you only need to deal with the logic of handling new and removed tiles.
3133

32-
This code snippet is from the example project.
34+
For the sake of completeness here's the setup:
3335

3436
```
35-
let update = grid.update(visibleRegion: region)
36-
print("update: +\(update.newTiles.count) -\(update.removedTiles.count)")
37-
38-
let newOverlays = update.newTiles.map { $0.item.overlay }
39-
mapView.addOverlays(newOverlays)
37+
struct Tile {
38+
let cities: [City]
39+
}
4040
41-
let removedOverlays = update.removedTiles.map { $0.item.overlay }
42-
mapView.removeOverlays(removedOverlays)
41+
func createTile(mapIndex: MapIndex, mapGrid: MapGrid<Tile>) -> Tile {
42+
let region = mapGrid.region(at: mapIndex)
43+
let cities = /* Get the cities for the region here */
44+
return Tile(cities: cities)
45+
}
4346
```
4447

45-
And that's it! With MapGrid you only need to deal with the logic of handling new and removed tiles.
48+
### Filling
49+
50+
With the grid you call `fill(toRegion:newTile:)` to get new tiles for the given region. As a result, you only get newly created tiles back and you need to update the UI with these tiles.
51+
52+
### Cropping
53+
54+
To remove tiles from the grid you use `crop(toRegion:)`. This will give you tiles that were removed from the grid and it is up to you to remove the contents of these tiles from the map.
4655

4756

4857
# TODO:
4958

50-
- Better example with annotations
5159
- Provide the origin in initialization
60+
- Implement animations for the demo
61+
- Tests
5262

0 commit comments

Comments
 (0)