Git Product home page Git Product logo

bentomap's Introduction

BentoMap

Map Clustering for Swift

Build Status Version License Platform Carthage compatible

BentoMap is an Swift implementation of quadtrees for map annotation clustering, and storage. It can also allow other 2d coordinate data to conform to a protocol and be added into BentoBox containers.

For more information, check out the Raizlabs Developer Blog. The Android equivalent, known as "Marker Clustering," is documented here.

BentoMap

Features

  • Store annotation data in QuadTrees
  • Fetch annotations in a region, with a clustering threshold
  • Protocols for storing other data types

Requirements

  • iOS 9.0+
  • Xcode 8.0

Installation

CocoaPods

BentoMap is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'BentoMap'

Carthage

Create a Cartfile that lists the framework and run carthage update. Follow the instructions to add $(SRCROOT)/Carthage/Build/iOS/BentoMap.framework to an iOS project.

github "Raizlabs/BentoMap"

Manually

  1. Download all of the .swift files in BentoMap/ and BentoMap/Extensions/ and drop them into your project.
  2. Congratulations!

Usage example

To see a full implementation of loading data into a map view, check out the example project.

Inserting Data

import BentoMap

static var sampleData: QuadTree<Int, MKMapRect, MKMapPoint> {
    var samples = QuadTree<Int, MKMapRect, MKMapPoint>(bentoBox: BentoBox(minPoint: MKMapPointForCoordinate(CLLocationCoordinate2D.minCoord), maxPoint: MKMapPointForCoordinate(CLLocationCoordinate2D.maxCoord)), bucketCapacity: 5)
    let randomData = (1...5000).map { count in
        return QuadTreeNode(originCoordinate: MKMapPointForCoordinate(CLLocationCoordinate2D.randomCoordinate()), content: count)
    }
    for node in randomData {
        samples.insertNode(node)
    }
    return samples
}

Updating a Map View

func updateAnnotations(inMapView mapView: MKMapView,
                                 forMapRect root: MKMapRect) {
    guard !mapView.frame.isEmpty && !MKMapRectIsEmpty(root) else {
        mapView.removeAnnotations(mapView.annotations)
        return
    }
    let zoomScale = Double(mapView.frame.width) / root.size.width
    let clusterResults = mapData.clusteredDataWithinMapRect(root,
                                    zoomScale: zoomScale,
                                    cellSize: Double(MapKitViewController.cellSize))
    let newAnnotations = clusterResults.map(BaseAnnotation.makeAnnotation)

    let oldAnnotations = mapView.annotations.flatMap({ $0 as? BaseAnnotation })

    let toRemove = oldAnnotations.filter { annotation in
        return !newAnnotations.contains { newAnnotation in
            return newAnnotation == annotation
        }
    }

    mapView.removeAnnotations(toRemove)

    let toAdd = newAnnotations.filter { annotation in
        return !oldAnnotations.contains { oldAnnotation in
            return oldAnnotation == annotation
        }
    }

    mapView.addAnnotations(toAdd)
}

Contributing

Issues and pull requests are welcome! Please ensure that you have the latest SwiftLint installed before committing and that there are no style warnings generated when building.

Contributors are expected to abide by the Contributor Covenant Code of Conduct.

License

BentoMap is available under the MIT license. See the LICENSE file for more info.

Authors

bentomap's People

Contributors

jvisenti avatar mattthousand avatar ateliercw avatar denisovillyacr avatar

Watchers

James Cloos avatar Denisov Ilya avatar

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.