-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocation Manager.swift
More file actions
59 lines (47 loc) · 2.41 KB
/
Location Manager.swift
File metadata and controls
59 lines (47 loc) · 2.41 KB
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
//
// File.swift
//
//
// Created by Dan Lages on 09/09/2018.
//
import Foundation
import CoreLocation
import Mapkit
//To be placed in View Did Load
locationManager.delegate = self //CLLocationManager Delegate
locationManager.requestLocation()
locationManager.requestWhenInUseAuthorization() //Only require information when app is in the forground
locationManager.startUpdatingLocation()
locationManager.distanceFilter = 100 //Only update distance information when user has moved given number of meters from previous update to improve efficiency
//Location Function
let locationManager = CLLocationManager() //Define Location manager object.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//Delegate function allows us to handle loaction information
let location:CLLocationCoordinate2D = manager.location!.coordinate //Determine user location
let userLat = location.latitude
let userLong = location.longitude
let userLocation = CLLocation(latitude: userLat, longitude: userLong)
//Convert data to readable 2D coordinate region
let span = MKCoordinateSpanMake(0.01, 0.01)
let currentLocal = userLocation.coordinate
let region = MKCoordinateRegionMake(currentLocal, span)
destinationMapView.setRegion(region, animated: true) // Represent User Location on map
self.destinationMapView.showsUserLocation = true
let geoCoder = CLGeocoder()
for dest in destinations {
// Determine location of each destination displayed in table view
let destinationAddress = (dest.addressNo + ", " + dest.addressStreet + ", " + dest.addressPostcode) //Group address variables to analyse the geo-location
geoCoder.geocodeAddressString(destinationAddress) {
placemarks, error in
let placemark = placemarks?.first
let destLat = placemark?.location?.coordinate.latitude
let destLong = placemark?.location?.coordinate.longitude
let destCoordinates = CLLocation(latitude: destLat!, longitude: destLong!) //Determine at set lat and long coordinates
let distance = String(format: "%2f km", destCoordinates.distance(from: userLocation) / 1000)
print ("Distance in KM is: \(distance)")
}
//Filter table based on closest location
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}