-
Notifications
You must be signed in to change notification settings - Fork 27
/
TrackOrderViewController.swift
125 lines (94 loc) · 3.69 KB
/
TrackOrderViewController.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// TrackOrderViewController.swift
// Foodsie
//
// Created by Cons Bulaqueña on 01/06/2018.
// Copyright © 2018 consios. All rights reserved.
//
import UIKit
import MapKit
import SwiftyJSON
class TrackOrderViewController : UIViewController
{
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var containerView: UIView!
@IBOutlet weak var progressBarView: UIView!
@IBOutlet weak var restaurantLabel: UILabel!
@IBOutlet weak var orderStatusLabel: UILabel!
@IBOutlet weak var menuBarButtonItem: UIBarButtonItem!
var order: Order!
var destination: MKPlacemark?
var source: MKPlacemark?
override func viewDidLoad() {
super.viewDidLoad()
//Menu Bar code
menuBarButtonItem.target = self.revealViewController()
menuBarButtonItem.action = #selector(SWRevealViewController.revealToggle(_: ))
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
mapView.delegate = self
getLatestOrder()
}
func getLatestOrder()
{
Order.getLatestOrder { (json) in
DispatchQueue.main.async {
let restaurantAddress = json["restaurant"]["address"].string!
let shippingAddress = json["adress"].string!
self.getLocation(restaurantAddress, title: "Restaurant", completion: { (sourceLocation) in
self.source = sourceLocation
self.getLocation(shippingAddress, title: "You", completion: { (destinationLocation) in
self.destination = destinationLocation
//draw directions route on map
self.getDirectionsOnMap()
})
})
}
}
}
}
extension TrackOrderViewController : MKMapViewDelegate
{
// convert a string address into location in map
func getLocation(_ address: String, title: String, completion: @escaping(MKPlacemark) -> Void)
{
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, error) in
if error != nil {
print("Error translating location into placemark: ", error)
}
if let placemark = placemarks?.first {
let coordinate = placemark.location!.coordinate
//create pin on the map
let pin = MKPointAnnotation()
pin.coordinate = coordinate
pin.title = title
self.mapView.addAnnotation(pin)
completion(MKPlacemark(placemark: placemark))
}
}
}
func getDirectionsOnMap()
{
let request = MKDirectionsRequest()
request.source = MKMapItem(placemark: source!)
request.destination = MKMapItem(placemark: destination!)
request.requestsAlternateRoutes = false
let directions = MKDirections(request: request)
directions.calculate { (response, error) in
if error != nil {
print("error calculating directions: ", error)
} else {
// show routes now
self.showRoute(response: response!)
}
}
}
func showRoute(response: MKDirectionsResponse)
{
for route in response.routes {
self.mapView.add(route.polyline, level: .aboveRoads)
}
//zoom map into route
var zoomRect = MKMapRectNull
}
}