Created
April 29, 2018 00:36
-
-
Save lfarah/e05cb12bc635fe8da0694959c1342f97 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// MapViewController.swift | |
// HackathonGlobo | |
// | |
// Created by Txai Wieser on 28/04/18. | |
// Copyright © 2018 Txai Wieser. All rights reserved. | |
// | |
import Foundation | |
import UIKit | |
import MapKit | |
import CoreLocation | |
class MapViewController: UIViewController { | |
@IBOutlet weak var mapView: MKMapView! | |
var manager: CLLocationManager! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// EventManager.getEvents { (events) in | |
for event in EventManager.shared.events { | |
let coordinate = CLLocationCoordinate2D(latitude: event.latitude, longitude: event.longitude) | |
let annotation = PinAnnotation(coordinate: coordinate, title: event.name, subtitle: event.description) | |
self.mapView.addAnnotation(annotation) | |
} | |
// } | |
manager = CLLocationManager() | |
manager.delegate = self | |
manager.startUpdatingLocation() | |
manager.desiredAccuracy = kCLLocationAccuracyBest | |
manager.requestAlwaysAuthorization() | |
if CLLocationManager.locationServicesEnabled() { | |
manager.startUpdatingLocation() | |
} | |
// mapv | |
// let coordinates: [CLLocation] = [CLLocation(latitude: 0.03, longitude: 0.03), | |
// CLLocation(latitude: 2.03, longitude: 0.53), | |
// CLLocation(latitude: 1.03, longitude: 0.3)] | |
// mapView.addAnnotations(coordinates.map { return Event(coordinate: $0.coordinate) }) | |
} | |
} | |
extension MapViewController: CLLocationManagerDelegate { | |
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { | |
let location = locations.last | |
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude) | |
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.001, longitudeDelta: 0.001)) | |
self.mapView.setRegion(region, animated: true) | |
} | |
} | |
extension MapViewController: MKMapViewDelegate { | |
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { | |
// Don't want to show a custom image if the annotation is the user's location. | |
guard !(annotation is MKUserLocation) else { | |
return nil | |
} | |
// Better to make this class property | |
let annotationIdentifier = "AnnotationIdentifier" | |
var annotationView: MKAnnotationView? | |
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) { | |
annotationView = dequeuedAnnotationView | |
annotationView?.annotation = annotation | |
} | |
else { | |
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) | |
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) | |
} | |
if let annotationView = annotationView { | |
// Configure your annotation view here | |
annotationView.canShowCallout = true | |
annotationView.image = #imageLiteral(resourceName: "AnnotationStar") | |
} | |
return annotationView | |
} | |
} | |
class PinAnnotation: NSObject, MKAnnotation { | |
var coordinate: CLLocationCoordinate2D | |
var title: String? | |
var subtitle: String? | |
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String) { | |
self.coordinate = coordinate | |
self.title = title | |
self.subtitle = subtitle | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment