Code Monkey home page Code Monkey logo

mapgrid's Introduction

MapGrid

MapGrid is stateful data structure that helps loading partial map data. It keeps track of loaded tiles and gives you the control to handle loading and unloading of tiles.

It is best described with an example that can be found in MapGridExample.

How does it work?

MapGrid is a grid data structure that can be used as an abstraction over the map. It splits the area into distinct rectangular boundaries, tiles. Because of the geometry of the Earth it assumes the cylindrical map projection and thus, the tile size will vary based on latitude. Longitudinal size of the tile will be constant.

Usage

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.

var grid = MapGrid<Tile>(tileSize: 100000 /* meters */)

And here's an example on how to use it.

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {

    let visibleRegion = mapView.region
    let removedTiles = grid.crop(toRegion: visibleRegion)
    let newTiles = grid.fill(toRegion: visibleRegion, newTile: self.createTile)
    print("update: +\(newTiles.count) -\(removedTiles.count)")
    
    mapView.addAnnotations(newTiles.flatMap { $0.item.cities })
    mapView.removeAnnotations(removedTiles.flatMap { $0.item.cities })
}

And that's it! With MapGrid you only need to deal with the logic of handling new and removed tiles.

For the sake of completeness here's the setup:

struct Tile {
    let cities: [City]
}

func createTile(mapIndex: MapIndex, mapGrid: MapGrid<Tile>) -> Tile {
    let region = mapGrid.region(at: mapIndex)
    let cities = /* Get the cities for the region here */
    return Tile(cities: cities)
}

Filling

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.

Cropping

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.

TODO:

  • Provide the origin in initialization
  • Implement animations for the demo
  • Tests

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.