-
Notifications
You must be signed in to change notification settings - Fork 1
/
MapOperation.swift
66 lines (57 loc) · 1.79 KB
/
MapOperation.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// MapOperation.swift
// Where2Help
//
// Created by Aaron Cruz on 4/9/16.
// Copyright © 2016 Where2Help. All rights reserved.
//
import Foundation
import CoreLocation
import MapKit
class MapOperation {
class func run(geo locatable: Locatable, mapView: MKMapView) {
let op = MapOperation()
op.process(locatable, mapView: mapView)
}
var locatable: Locatable!
var zoom: Double {
get {
if locatable.hasMarker() {
return 0.02
}
return 0.05
}
}
func process(locatable: Locatable, mapView: MKMapView) {
self.locatable = locatable
let coords = locatable.toCoords()
let loc = makeLocation(coords)
removeAnnotations(mapView)
setCenter(loc, mapView: mapView)
if locatable.hasMarker() {
let m = marker(loc)
mapView.addAnnotation(m)
}
}
private func removeAnnotations(mapView: MKMapView) {
for anno in mapView.annotations {
mapView.removeAnnotation(anno)
}
}
private func makeLocation(coords: Coords) -> CLLocationCoordinate2D {
return CLLocationCoordinate2DMake(coords.lat, coords.lng)
}
private func setCenter(loc: CLLocationCoordinate2D, mapView: MKMapView) {
let span = MKCoordinateSpanMake(zoom, zoom)
let region = MKCoordinateRegionMake(loc, span)
mapView.setRegion(region, animated: false)
// mapView.setCenterCoordinate(loc, animated: true)
}
private func marker(loc: CLLocationCoordinate2D) -> MKAnnotation {
let annotation = MKPointAnnotation()
annotation.coordinate = loc
// annotation.title = "Roatan"
// annotation.subtitle = "Honduras"
return annotation
}
}