├── .gitignore ├── GeoJSONTileOverlay.swift ├── GeoJSONTileOverlayRenderer.swift ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | xcuserdata 13 | *.xccheckout 14 | *.moved-aside 15 | DerivedData 16 | *.hmap 17 | *.ipa 18 | *.xcuserstate 19 | 20 | # CocoaPods 21 | # 22 | # We recommend against adding the Pods directory to your .gitignore. However 23 | # you should judge for yourself, the pros and cons are mentioned at: 24 | # http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control 25 | # 26 | # Pods/ 27 | 28 | # Carthage 29 | # 30 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 31 | # Carthage/Checkouts 32 | 33 | Carthage/Build 34 | -------------------------------------------------------------------------------- /GeoJSONTileOverlay.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import MapKit 3 | 4 | class GeoJSONTileOverlay: MKTileOverlay { 5 | var polylines: [MKPolyline] = [] 6 | let queue = NSOperationQueue() 7 | 8 | override func loadTileAtPath(path: MKTileOverlayPath, result: ((NSData!, NSError!) -> Void)!) { 9 | if (result == nil) { 10 | return 11 | } 12 | let request = NSURLRequest(URL: self.URLForTilePath(path)) 13 | NSURLConnection.sendAsynchronousRequest(request, queue: queue, 14 | completionHandler: { 15 | (response: NSURLResponse!, data: NSData!, httpError: NSError!) -> Void in 16 | if let error = httpError { 17 | return result(data, error) 18 | } 19 | var jsonError: NSError? 20 | let jsonObject: AnyObject! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &jsonError) 21 | if let error = jsonError { 22 | return result(data, error) 23 | } 24 | let dataString = NSString(data: data, encoding: NSUTF8StringEncoding) 25 | if let features = (jsonObject as? NSDictionary)?["features"] as? NSArray { 26 | for feature in features { 27 | if let geometry = (feature as? NSDictionary)?["geometry"] as? NSDictionary { 28 | self.addGeometry(geometry) 29 | } 30 | } 31 | } 32 | result(data, nil) 33 | }) 34 | } 35 | 36 | func addGeometry(geometry: NSDictionary) { 37 | if let coordinates = geometry["coordinates"] as? NSArray { 38 | if let type = geometry["type"] as? NSString { 39 | if type == "LineString" { 40 | self.addLineString(coordinates) 41 | } else if type == "MultiLineString" { 42 | for lineString in coordinates { 43 | self.addLineString(lineString as? NSArray) 44 | } 45 | } 46 | } 47 | } 48 | } 49 | 50 | func addLineString(geoJsonCoordinates: NSArray!) { 51 | var polylineCoordinates: [CLLocationCoordinate2D] = [] 52 | polylineCoordinates.reserveCapacity(geoJsonCoordinates.count) 53 | for geoJsonCoordinate in geoJsonCoordinates { 54 | let longitude: CLLocationDegrees = geoJsonCoordinate[0].doubleValue 55 | let latitude: CLLocationDegrees = geoJsonCoordinate[1].doubleValue 56 | polylineCoordinates.append(CLLocationCoordinate2DMake(latitude, longitude)) 57 | } 58 | let polyline = MKPolyline(coordinates: &polylineCoordinates, count: polylineCoordinates.count) 59 | self.polylines.append(polyline) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /GeoJSONTileOverlayRenderer.swift: -------------------------------------------------------------------------------- 1 | import Foundation 2 | import MapKit 3 | 4 | class GeoJSONTileOverlayRenderer: MKTileOverlayRenderer { 5 | var color: UIColor = UIColor.blackColor() 6 | var width: CGFloat = 0.1 7 | 8 | override func drawMapRect(mapRect: MKMapRect, zoomScale: MKZoomScale, inContext context: CGContext!) { 9 | let jsonOverlay = self.overlay as! GeoJSONTileOverlay 10 | for polyline in jsonOverlay.polylines { 11 | if (polyline.pointCount >= 2) { 12 | var path = CGPathCreateMutable() 13 | self.createLines(polyline, withPath: path) 14 | CGContextBeginPath(context) 15 | CGContextAddPath(context, path) 16 | let lineWidth: CGFloat = width / zoomScale 17 | CGContextSetLineWidth(context, lineWidth / zoomScale) 18 | CGContextSetLineJoin(context, kCGLineJoinRound) 19 | CGContextSetLineCap(context, kCGLineCapRound) 20 | CGContextSetStrokeColorWithColor(context, color.CGColor) 21 | CGContextStrokePath(context) 22 | //CGPathRelease(path) 23 | } 24 | } 25 | } 26 | 27 | func createLines(polyline: MKPolyline, withPath path: CGMutablePathRef) { 28 | let points = polyline.points() 29 | var relativePoint = pointForMapPoint(points[0]) 30 | CGPathMoveToPoint(path, nil, relativePoint.x, relativePoint.y) 31 | for var i = 1; i < polyline.pointCount; ++i { 32 | relativePoint = pointForMapPoint(points[i]) 33 | CGPathAddLineToPoint(path, nil, relativePoint.x, relativePoint.y) 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Halvard Skogsrud 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GeoJSONTileOverlay for iOS 2 | 3 | If you have a [TileStache](http://tilestache.org/) server providing [vector tiles](http://tilestache.org/doc/#vector-provider) in [GeoJSON](http://geojson.org/geojson-spec.html) format, these classes will allow you to render this data on iOS devices running iOS 7.0+ using MapKit. 4 | 5 | Use them just as you would the built-in [MKTileOverlay](https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKTileOverlay_class/index.html) and [MKTileOverlayRenderer](https://developer.apple.com/library/ios/documentation/MapKit/Reference/MKTileOverlayRenderer_class/index.html) classes. 6 | 7 | ## Limitations 8 | 9 | * Only handles [LineString](http://geojson.org/geojson-spec.html#linestring)s and [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring)s, ignores all other geometry types. 10 | 11 | --------------------------------------------------------------------------------