 import UIKit
 import MapKit
 
-class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
+class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UISearchBarDelegate {
 
 	// MARK: - Outlets
 	

		let currentLocationButton = UIBarButtonItem(title: "Current Location", style: UIBarButtonItemStyle.Plain, target: self, action: "currentLocationButtonAction:")
		self.navigationItem.leftBarButtonItem = currentLocationButton
		
		mapView.delegate = self
		
		activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)

			locationManager.desiredAccuracy = kCLLocationAccuracyBest
			locationManager.requestAlwaysAuthorization()
			locationManager.startUpdatingLocation()
		}
	}
	
	func searchButtonAction(button: UIBarButtonItem) {
		if searchController == nil {
			searchController = UISearchController(searchResultsController: nil)
		}
		searchController.hidesNavigationBarDuringPresentation = false
		self.searchController.searchBar.delegate = self
		presentViewController(searchController, animated: true, completion: nil)
	}
	
	func searchBarSearchButtonClicked(searchBar: UISearchBar){
		searchBar.resignFirstResponder()
		dismissViewControllerAnimated(true, completion: nil)
		if self.mapView.annotations.count != 0 {
			annotation = self.mapView.annotations[0]
			self.mapView.removeAnnotation(annotation)
		}
		
		localSearchRequest = nil
		localSearch = nil
		localSearchResponse = nil
		
		localSearchRequest = MKLocalSearchRequest()
		localSearchRequest.naturalLanguageQuery = searchBar.text
		localSearch = MKLocalSearch(request: localSearchRequest)
		localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in
			
			if localSearchResponse == nil {
				let alert = UIAlertView(title: nil, message: NSLocalizedString("PLACE_NOT_FOUND", comment: ""), delegate: self, cancelButtonTitle: NSLocalizedString("TRY_AGAIN", comment: ""))
				alert.show()
				return
			}
			
			let pointAnnotation = MKPointAnnotation()
			pointAnnotation.title = searchBar.text
			pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude:     localSearchResponse!.boundingRegion.center.longitude)
			
			let pinAnnotationView = MKPinAnnotationView(annotation: pointAnnotation, reuseIdentifier: nil)
			self.mapView.centerCoordinate = pointAnnotation.coordinate
			self.mapView.addAnnotation(pinAnnotationView.annotation!)
		}
	}
	
