// // LokasiBalaiUjiVC.swift // Sirani // // Created by Anhar Solehudin on 05/04/19. // Copyright © 2019 Anhar Solehudin. All rights reserved. // import UIKit import GoogleMaps import MapKit class LokasiBalaiUjiVC: UIViewController { @IBOutlet weak var mapView: GMSMapView! @IBOutlet weak var btnDirection: UIButton! private var locationManager = CLLocationManager() private var currentLocation: CLLocation? private var zoomLevel: Float = 7.0 private var userMarker: GMSMarker? private var presenter: LokasiBalaiUjiPresenter! private var dataList: [BalaiUji] = [BalaiUji]() private var loading = false // View Lifecycles override var preferredStatusBarStyle: UIStatusBarStyle { return .lightContent } override func viewDidLoad() { super.viewDidLoad() locationManager = CLLocationManager() locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestWhenInUseAuthorization() locationManager.distanceFilter = 50 locationManager.startUpdatingLocation() locationManager.delegate = self presenter = LokasiBalaiUjiPresenter(siraniApiServices: SiraniApiServices()) } override func viewWillAppear(_ animated: Bool) { presenter.attachView(view: self) btnDirection.bringSubviewToFront(mapView) btnDirection.layer.cornerRadius = 5 btnDirection.layer.borderWidth = 0.5 btnDirection.layer.borderColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1) } override func viewDidDisappear(_ animated: Bool) { presenter.detachView() } private func loadData() { if !loading { if ConnectionHelper.isConnectedToNetwork() { presenter.loadData() } } } @IBAction func onBtnBackTap(_ sender: Any) { performSegue(withIdentifier: "unwindLokasiBalaiUjiToBalaiUji", sender: self) } @IBAction func onBtnDirectionTap(_ sender: Any) { if let selectedMarker = mapView.selectedMarker { var markerData: String = "lokasi-balai-uji" if selectedMarker.userData != nil { markerData = selectedMarker.userData as! String } if markerData != "user-tag" { let position = selectedMarker.position let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: position, addressDictionary:nil)) mapItem.name = selectedMarker.title ?? "Lokasi Balai Uji" mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]) } } } } extension LokasiBalaiUjiVC: CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { switch status { case .restricted: print("Location access was restricted.") case .denied: print("User denied access to location.") case .notDetermined: print("Location status not determined.") case .authorizedAlways: fallthrough case .authorizedWhenInUse: print("Location status is OK.") locationManager.startUpdatingLocation() mapView.isMyLocationEnabled = false mapView.settings.myLocationButton = false @unknown default: print("Error Load Map") } } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let location = locations.first else { return } mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: zoomLevel, bearing: 0, viewingAngle: 0) locationManager.stopUpdatingLocation() currentLocation = location // update user marker userMarker = GMSMarker() userMarker?.icon = UIImage(named: "user-marker") userMarker?.position = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude) userMarker?.title = "Anda Sedang di Lokasi Ini" userMarker?.map = mapView userMarker?.userData = "user-tag" mapView.selectedMarker = userMarker // load data loadData() } func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { locationManager.stopUpdatingLocation() print("Error: \(error)") } } extension LokasiBalaiUjiVC: LokasiBalaiUjiViewDelegate { func showLoading() { loading = true } func hideLoading() { loading = false } func setDataList(dataList: [BalaiUji]) { for data in dataList { self.dataList.append(data) // add marker (latitude masih tertukar dgn longitud dr source data) let dlat = Double(data.longitude) let dlng = Double(data.latitude) let lat = CLLocationDegrees(exactly: dlat!) let lng = CLLocationDegrees(exactly: dlng!) let balaiUjiMarker = GMSMarker() balaiUjiMarker.icon = GMSMarker.markerImage(with: #colorLiteral(red: 0.1411764771, green: 0.3960784376, blue: 0.5647059083, alpha: 1)) balaiUjiMarker.position = CLLocationCoordinate2D(latitude: lat!, longitude: lng!) balaiUjiMarker.title = data.nama if let userLocation = self.currentLocation { let lokasiBalaiUji = CLLocation(latitude: dlat!, longitude: dlng!) let distanceInMeters = lokasiBalaiUji.distance(from: userLocation) let distanceInKm: Int = (Int(distanceInMeters/1000)) balaiUjiMarker.snippet = "Jarak \(distanceInKm) km" } balaiUjiMarker.map = mapView } } }