Created
June 17, 2015 02:53
-
-
Save damianesteban/520058de6ce874e5efbf to your computer and use it in GitHub Desktop.
MKMapViewController
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
import UIKit | |
import MapKit | |
import Parse | |
class MapViewController: UIViewController, CLLocationManagerDelegate, | |
MKMapViewDelegate, UISearchBarDelegate { | |
override func shouldAutorotate() -> Bool { | |
return false | |
} | |
override func supportedInterfaceOrientations() -> Int { | |
return Int(UIInterfaceOrientationMask.Portrait.rawValue) | |
} | |
let locationManager = CLLocationManager() | |
let searchController = UISearchController(searchResultsController: nil) | |
var locationHasBeenUpdated = false | |
var currentSearch: String? | |
var visibleRectangle: MKMapRect? | |
var selectedPlaceName: String? | |
var selectedLocation: CLLocation? | |
@IBOutlet weak var mapView: MKMapView! | |
@IBOutlet weak var toolbar: UIToolbar! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
locationManager.delegate = self | |
mapView.delegate = self | |
mapView.showsUserLocation = true | |
let barItem = MKUserTrackingBarButtonItem(mapView: mapView) | |
var barItems = toolbar.items as? [UIBarButtonItem] | |
barItems?.insert(barItem, atIndex: 0) | |
toolbar.setItems(barItems, animated: true) | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
// MARK: - IBActions | |
@IBAction func search(sender: UIBarButtonItem) { | |
searchController.hidesNavigationBarDuringPresentation = true | |
searchController.searchBar.delegate = self | |
presentViewController(searchController, animated: true, completion: nil) | |
} | |
@IBAction func redoSearch(sender: UIBarButtonItem) { | |
if let search = currentSearch, oldRect = visibleRectangle { | |
/* | |
let oldRegion: MKCoordinateRegion = MKCoordinateRegionForMapRect(oldRect) | |
let oldRegionCenterPoint: MKMapPoint = MKMapPointForCoordinate(oldRegion.center) | |
let smallerRect: MKMapRect = MKMapRectInset(mapView.visibleMapRect, 1000, 3000) | |
let isPointInRect: Bool = MKMapRectContainsPoint(smallerRect, oldRegionCenterPoint) | |
if !isPointInRect { | |
findPlaces(search) | |
} | |
*/ | |
clearPins(false) | |
findPlaces(search) | |
} | |
} | |
@IBAction func clearMap(sender: UIBarButtonItem) { | |
clearPins(true) | |
} | |
// MARK: - UISearchBarDelegate | |
func searchBarSearchButtonClicked(searchBar: UISearchBar) { | |
searchBar.resignFirstResponder() | |
dismissViewControllerAnimated(true, completion: nil) | |
clearPins(true) | |
currentSearch = searchBar.text | |
findPlaces(currentSearch!) | |
} | |
/* | |
// MARK: - MKMapViewDelegate | |
*/ | |
func mapView(mapView: MKMapView!, | |
viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { | |
let id: String = annotation.title! | |
let pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: id) | |
if id != "Current Location" { | |
let pin = MKPinAnnotationView( | |
annotation: annotation, | |
reuseIdentifier: id | |
) | |
pin.rightCalloutAccessoryView = | |
UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIView | |
pin.animatesDrop = true | |
pin.canShowCallout = true | |
return pin | |
} else { | |
return nil | |
} | |
} | |
func mapView(mapView: MKMapView!, | |
didUpdateUserLocation userLocation: MKUserLocation!) { | |
if !locationHasBeenUpdated { | |
let region: MKCoordinateRegion = MKCoordinateRegionForMapRect(mapView.visibleMapRect) | |
let centerOfRegion: CLLocationCoordinate2D = userLocation.location.coordinate | |
let newSpan = MKCoordinateSpan(latitudeDelta: region.span.latitudeDelta / 5000, | |
longitudeDelta: region.span.longitudeDelta / 5000) | |
let zoomedInRegion = MKCoordinateRegion(center: centerOfRegion, span: newSpan) | |
mapView.setRegion(zoomedInRegion, animated: true) | |
locationHasBeenUpdated = true | |
} | |
} | |
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, | |
calloutAccessoryControlTapped control: UIControl!) { | |
selectedPlaceName = view.annotation.title | |
let coords: CLLocationCoordinate2D = view.annotation.coordinate | |
selectedLocation = CLLocation(latitude: coords.latitude, longitude: coords.longitude) | |
performSegueWithIdentifier("showPhotoGrid", sender: nil) | |
} | |
// MARK: - Map Utilities | |
func findPlaces(query: String) { | |
visibleRectangle = mapView.visibleMapRect | |
let smallerRect: MKMapRect = MKMapRectInset(visibleRectangle!, 500, 2500) | |
let visibleRegion: MKCoordinateRegion = MKCoordinateRegionForMapRect(smallerRect) | |
let searchRequest = MKLocalSearchRequest() | |
searchRequest.naturalLanguageQuery = query | |
searchRequest.region = visibleRegion | |
let mapSearch = MKLocalSearch(request: searchRequest) | |
mapSearch.startWithCompletionHandler({ (searchResponse, error) -> Void in | |
let placesFound = searchResponse?.mapItems as? [MKMapItem] | |
var annotations = [MapAnnotation]() | |
if let placesFound = placesFound { | |
for place in placesFound { | |
let annotation = MapAnnotation( | |
coordinate: place.placemark.coordinate, | |
title: place.placemark.name | |
) | |
annotations.append(annotation) | |
} | |
} else { | |
var alert = UIAlertView( | |
title: nil, message: "Place not found", | |
delegate: self, cancelButtonTitle: "Try different search" | |
) | |
alert.show() | |
} | |
self.mapView.addAnnotations(annotations) | |
self.mapView.showAnnotations(annotations, animated: true) | |
}) | |
} | |
func clearPins(clearSearch: Bool) { | |
if clearSearch { | |
currentSearch = nil | |
} | |
if mapView.annotations.count > 0 { | |
mapView.removeAnnotations(mapView.annotations) | |
} | |
} | |
// MARK: - Navigation | |
// In a storyboard-based application, you will often want to do a little preparation before navigation | |
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { | |
if segue.identifier == "showPhotoGrid" { | |
var destinationViewController = segue.destinationViewController as! SeePhotoGridCollectionViewController | |
destinationViewController.selectedPlaceName = selectedPlaceName | |
destinationViewController.selectedLocation = selectedLocation | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment