-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get Geohashes in MKCoordinateRegion (#3)
* Get Geohashes in an MKCoordinateRegion
- Loading branch information
Showing
7 changed files
with
279 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//: A MapKit based Playground | ||
|
||
import MapKit | ||
import PlaygroundSupport | ||
import GeohashKit | ||
|
||
// MARK: - Boilerplate for MapView delegate | ||
class PlaygroundMapViewDelegate: NSObject, MKMapViewDelegate { | ||
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { | ||
if let polygon = overlay as? GeohashPolygon { | ||
let renderer = MKPolygonRenderer(polygon: polygon) | ||
renderer.strokeColor = .black | ||
renderer.lineWidth = 3.0 | ||
renderer.fillColor = UIColor.purple.withAlphaComponent(0.5) | ||
|
||
return renderer | ||
} | ||
|
||
if let polygon = overlay as? MKPolygon { | ||
let renderer = MKPolygonRenderer(polygon: polygon) | ||
renderer.strokeColor = .black | ||
renderer.lineWidth = 1.5 | ||
renderer.fillColor = UIColor.green.withAlphaComponent(0.3) | ||
|
||
return renderer | ||
} | ||
|
||
fatalError() | ||
} | ||
} | ||
|
||
class GeohashPolygon: MKPolygon { | ||
var geohash: Geohash! | ||
|
||
static func create(_ geohash: Geohash) -> GeohashPolygon { | ||
let instance = GeohashPolygon(coordinateRegion: geohash.region) | ||
instance.geohash = geohash | ||
return instance | ||
} | ||
} | ||
|
||
// MARK: - Playground Code | ||
|
||
// Create a MKMapView | ||
let mapViewDelegate = PlaygroundMapViewDelegate() | ||
let mapView = MKMapView(frame: CGRect(x: 0, y: 0, width: 800, height: 800)) | ||
mapView.delegate = mapViewDelegate | ||
|
||
// Add Seattle Capitol Hill overlay | ||
let capitolHill = MKMapRect( | ||
x: 43000993.632010244, | ||
y: 93716493.6709905, | ||
width: 15247.761742688715, | ||
height: 24753.363981068134 | ||
) | ||
|
||
let capitolHillRegion = MKCoordinateRegion(capitolHill) | ||
let capitolHillPolygon = MKPolygon(coordinateRegion: capitolHillRegion) | ||
|
||
mapView.setRegion(capitolHillRegion, animated: true) | ||
mapView.addOverlay(capitolHillPolygon, level: .aboveRoads) | ||
|
||
let regions = capitolHillRegion.geohashes(precision: 7) | ||
mapView.addOverlays(regions.map(GeohashPolygon.create), level: .aboveLabels) | ||
|
||
// Add the created mapView to our Playground Live View | ||
PlaygroundPage.current.liveView = mapView |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import MapKit | ||
|
||
extension MKPolygon { | ||
public convenience init(coordinateRegion: MKCoordinateRegion) { | ||
let coordinates = coordinateRegion.cornerCoordinates | ||
self.init(coordinates: coordinates, count: coordinates.count) | ||
} | ||
} | ||
|
||
extension MKCoordinateRegion { | ||
public var cornerCoordinates: [CLLocationCoordinate2D] { | ||
var points: [CLLocationCoordinate2D] = [] | ||
|
||
let sw = CLLocationCoordinate2D( | ||
latitude: center.latitude - (span.latitudeDelta / 2.0), | ||
longitude: center.longitude - (span.longitudeDelta / 2.0) | ||
) | ||
points.append(sw) | ||
|
||
let nw = CLLocationCoordinate2D( | ||
latitude: center.latitude + (span.latitudeDelta / 2.0), | ||
longitude: center.longitude - (span.longitudeDelta / 2.0) | ||
) | ||
points.append(nw) | ||
|
||
let ne = CLLocationCoordinate2D( | ||
latitude: center.latitude + (span.latitudeDelta / 2.0), | ||
longitude: center.longitude + (span.longitudeDelta / 2.0) | ||
) | ||
points.append(ne) | ||
|
||
let se = CLLocationCoordinate2D( | ||
latitude: center.latitude - (span.latitudeDelta / 2.0), | ||
longitude: center.longitude + (span.longitudeDelta / 2.0) | ||
) | ||
points.append(se) | ||
|
||
return points | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Timeline | ||
version = "3.0"> | ||
<TimelineItems> | ||
</TimelineItems> | ||
<TimelineItems> | ||
</TimelineItems> | ||
<TimelineItems> | ||
</TimelineItems> | ||
</Timeline> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import XCTest | ||
@testable import GeohashKit | ||
|
||
#if canImport(MapKit) | ||
import MapKit | ||
|
||
final class GeohashMapKitTests: XCTestCase { | ||
func testHashesInRegion() { | ||
let captiolHill = MKMapRect( | ||
x: 43000993.632010244, | ||
y: 93716493.6709905, | ||
width: 15247.761742688715, | ||
height: 24753.363981068134 | ||
) | ||
|
||
let region = MKCoordinateRegion(captiolHill) | ||
|
||
// Test high-precision | ||
XCTAssertGeohashesEqual( | ||
region.geohashes(precision: 6), | ||
["c23nbg", "c23nbm", "c23nbu", "c23nbe", "c23nby", "c23nbx", "c23nbw", "c23nbk", "c23nbz", "c23nbq", "c23nbr", "c23nbv", "c23nbt", "c23nb7", "c23nbs"] | ||
) | ||
|
||
XCTAssertGeohashesEqual( | ||
region.geohashes(precision: 5), | ||
["c23nb"] | ||
) | ||
} | ||
|
||
private func XCTAssertGeohashesEqual(_ lhs: Set<Geohash>, _ rhs: Set<String>, _ message: String = "") { | ||
let rhsGeohashes = rhs.compactMap(Geohash.init(geohash:)) | ||
XCTAssertEqual(lhs, Set(rhsGeohashes), message) | ||
} | ||
|
||
static var allTests = [ | ||
("testHashesInRegion", testHashesInRegion) | ||
] | ||
} | ||
|
||
#endif |