Created
January 19, 2018 16:15
-
-
Save codyeatworld/34936d15ff92aa38984499f780c66cd5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// Trailer | |
// | |
// Created by Cody Lopez on 1/18/18. | |
// Copyright © 2018 Cody Lopez. All rights reserved. | |
// | |
import UIKit | |
import Mapbox | |
import CoreLocation | |
import RxSwift | |
class ViewController: UIViewController, CLLocationManagerDelegate, MGLMapViewDelegate { | |
let locationManager = CLLocationManager() | |
let disposeBag = DisposeBag() | |
let coordinates$: PublishSubject<Array<Double>> = PublishSubject<Array<Double>>() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do something | |
enableBasicLocationServices() | |
let mapView = MGLMapView(frame: view.bounds, styleURL: MGLStyle.outdoorsStyleURL()) | |
mapView.delegate = self | |
mapView.showsUserLocation = true | |
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight] | |
let subscription = coordinates$.subscribe(onNext: { coordinates in | |
mapView.setCenter(CLLocationCoordinate2D(latitude: coordinates[0], longitude: coordinates[1]), zoomLevel: 9, animated: false) | |
self.view.addSubview(mapView) | |
print("\(coordinates)") | |
}) | |
subscription.disposed(by: disposeBag) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func enableBasicLocationServices() { | |
locationManager.delegate = self | |
switch CLLocationManager.authorizationStatus() { | |
case .notDetermined: | |
// Request when-in-use authorization initially | |
locationManager.requestWhenInUseAuthorization() | |
break | |
case .restricted, .denied: | |
// Disable location features | |
disableMyLocationBasedFeatures() | |
break | |
case .authorizedWhenInUse, .authorizedAlways: | |
// Enable location features | |
enableMyWhenInUseFeatures() | |
break | |
} | |
} | |
func locationManager(_ manager: CLLocationManager, | |
didChangeAuthorization status: CLAuthorizationStatus) { | |
switch status { | |
case .restricted, .denied: | |
disableMyLocationBasedFeatures() | |
break | |
case .authorizedWhenInUse: | |
enableMyWhenInUseFeatures() | |
break | |
case .notDetermined, .authorizedAlways: | |
break | |
} | |
} | |
func disableMyLocationBasedFeatures(){ | |
print("disableMyLocationBasedFeatures") | |
} | |
func enableMyWhenInUseFeatures(){ | |
print("enableMyWhenInUseFeaatures") | |
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters | |
locationManager.distanceFilter = 2.5 // In meters. | |
locationManager.startUpdatingLocation() | |
} | |
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | |
let coordinates = locations.last!.coordinate | |
coordinates$.onNext([coordinates.latitude, coordinates.longitude]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment