Skip to content

Commit c9c5af8

Browse files
committed
Example uses Open Street Maps and CachedTileOverlay
1 parent ff68602 commit c9c5af8

8 files changed

Lines changed: 185 additions & 22 deletions

File tree

Example/MapCache/Base.lproj/Main.storyboard

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
77
<deployment identifier="iOS"/>
8-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13772"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
99
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
1010
</dependencies>
1111
<scenes>
@@ -20,11 +20,21 @@
2020
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
2121
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
2222
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
23+
<subviews>
24+
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" fixedFrame="YES" mapType="standard" showsUserLocation="YES" translatesAutoresizingMaskIntoConstraints="NO" id="dwC-V5-0co" userLabel="Map">
25+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
26+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
27+
</mapView>
28+
</subviews>
2329
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
2430
</view>
31+
<connections>
32+
<outlet property="map" destination="dwC-V5-0co" id="Ir8-jR-9rJ"/>
33+
</connections>
2534
</viewController>
2635
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
2736
</objects>
37+
<point key="canvasLocation" x="80.799999999999997" y="114.69265367316342"/>
2838
</scene>
2939
</scenes>
3040
</document>

Example/MapCache/ViewController.swift

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,51 @@
77
//
88

99
import UIKit
10+
import MapKit
11+
import CoreLocation
12+
import MapCache
1013

1114
class ViewController: UIViewController {
12-
15+
16+
@IBOutlet weak var map: MKMapView!
17+
1318
override func viewDidLoad() {
1419
super.viewDidLoad()
15-
// Do any additional setup after loading the view, typically from a nib.
20+
21+
//Set delegate
22+
map.delegate = self
23+
24+
//Request permissions
25+
//Config
26+
let config = MapCacheConfig(withTileUrlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png")
27+
28+
29+
let tileServerOverlay = CachedTileOverlay(mapCacheConfig: config)
30+
tileServerOverlay.canReplaceMapContent = true
31+
map.insert(tileServerOverlay, at: 0, level: .aboveLabels)
1632
}
1733

1834
override func didReceiveMemoryWarning() {
1935
super.didReceiveMemoryWarning()
2036
// Dispose of any resources that can be recreated.
2137
}
38+
}
2239

40+
extension ViewController : MKMapViewDelegate {
41+
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
42+
if overlay.isKind(of: MKTileOverlay.self) {
43+
return MKTileOverlayRenderer(overlay: overlay)
44+
}
45+
return MKOverlayRenderer()
46+
}
2347
}
2448

49+
extension ViewController : CLLocationManagerDelegate {
50+
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
51+
//Hello
52+
}
53+
54+
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
55+
// Hello
56+
}
57+
}

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 23 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MapCache.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ A smart cache for iOS Map Applications.
3737
# }
3838

3939
# s.public_header_files = 'Pod/Classes/**/*.h'
40-
# s.frameworks = 'UIKit', 'MapKit'
40+
s.frameworks = 'UIKit', 'MapKit'
4141
# s.dependency 'AFNetworking', '~> 2.3'
4242
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// CachedTileOverlay.swift
3+
//
4+
// Base source code comes from Open GPX Tracker http://github.com/iOS-Open-GPX-Tracker
5+
//
6+
//
7+
8+
import Foundation
9+
import MapKit
10+
11+
12+
///
13+
/// Overwrites the default overlay to store downloaded images
14+
///
15+
public class CachedTileOverlay : MKTileOverlay {
16+
17+
/// Tells loadTile method if the tile shall be loaded rom the app cache.
18+
public var useCache: Bool = true
19+
20+
21+
public var config: MapCacheConfig?
22+
23+
public init(mapCacheConfig: MapCacheConfig) {
24+
super.init(urlTemplate: mapCacheConfig.tileUrlTemplate)
25+
self.config = mapCacheConfig
26+
}
27+
28+
///
29+
/// Generates the URL for the tile to be requested.
30+
/// It replaces the values of {z},{x} and {y} in the urlTemplate defined in GPXTileServer
31+
///
32+
/// -SeeAlso: GPXTileServer
33+
///
34+
override public func url(forTilePath path: MKTileOverlayPath) -> URL {
35+
//print("CachedTileOverlay:: url() urlTemplate: \(urlTemplate)")
36+
var urlString = urlTemplate?.replacingOccurrences(of: "{z}", with: String(path.z))
37+
urlString = urlString?.replacingOccurrences(of: "{x}", with: String(path.x))
38+
urlString = urlString?.replacingOccurrences(of: "{y}", with: String(path.y))
39+
40+
//get random subdomain
41+
let subdomains = "abc"
42+
let rand = arc4random_uniform(UInt32(subdomains.count))
43+
let randIndex = subdomains.index(subdomains.startIndex, offsetBy: String.IndexDistance(rand));
44+
urlString = urlString?.replacingOccurrences(of: "{s}", with:String(subdomains[randIndex]))
45+
print("CachedTileOverlay:: url() urlString: \(urlString ?? "no url")")
46+
return URL(string: urlString!)!
47+
}
48+
49+
///
50+
/// Loads the tile from the network or from cache
51+
///
52+
/// If the internal app cache is activated,it tries to get the tile from it.
53+
/// If not, it uses the default system cache (managed by the OS).
54+
///
55+
override public func loadTile(at path: MKTileOverlayPath,
56+
result: @escaping (Data?, Error?) -> Void) {
57+
let url = self.url(forTilePath: path)
58+
print ("CachedTileOverlay::loadTile() url=\(url) useCache: \(useCache)")
59+
60+
if !self.useCache {
61+
print("lay:: not using cache")
62+
return super.loadTile(at: path, result: result)
63+
}
64+
// Use cache
65+
66+
return super.loadTile(at: path, result: result)
67+
}
68+
}

MapCache/Classes/MapCache.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// MapCache.swift
3+
// MapCache
4+
//
5+
// Created by merlos on 13/05/2019.
6+
//
7+
8+
import Foundation
9+
10+
/// The real brain
11+
public class MapCache : NSObject {
12+
13+
override init() {
14+
super.init()
15+
}
16+
17+
func getTile(_ tileUrl: String) {
18+
}
19+
20+
func removeTile(_ tileUrl: String) {
21+
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// MapCacheConfig.swift
3+
// MapCache
4+
//
5+
// Created by merlos on 13/05/2019.
6+
//
7+
8+
import Foundation
9+
10+
public class MapCacheConfig : NSObject {
11+
12+
var tileUrlTemplate: String?
13+
14+
override public init() {
15+
super.init()
16+
}
17+
18+
public init(withTileUrlTemplate: String) {
19+
super.init()
20+
tileUrlTemplate = withTileUrlTemplate
21+
}
22+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pod 'MapCache'
2222

2323
## Author
2424

25-
merlos, merlos@gmail.com
25+
merlos
2626

2727
## License
2828

0 commit comments

Comments
 (0)