Skip to content

Instantly share code, notes, and snippets.

@RRCummins
Created February 2, 2016 14:52
Show Gist options
  • Save RRCummins/a3a723b65b9455dcb813 to your computer and use it in GitHub Desktop.
Save RRCummins/a3a723b65b9455dcb813 to your computer and use it in GitHub Desktop.
Find Coffee Now with MKLocalSearchRequest()
import UIKit
import MapKit
import CoreLocation
class MapClass: UIViewController, CLLocationManagerDelegate, UISearchBarDelegate {
var searchController:UISearchController!
var annotation:MKAnnotation!
var localSearchRequest:MKLocalSearchRequest!
var localSearch:MKLocalSearch!
var localSearchResponse:MKLocalSearchResponse!
var error:NSError!
var pointAnnotation:MKPointAnnotation!
var pinAnnotationView:MKPinAnnotationView!
@IBOutlet weak var mapView: MKMapView!
var locationManager: CLLocationManager!
let searchRadius: CLLocationDistance = 2500
override func viewDidLoad() {
super.viewDidLoad()
if (CLLocationManager.locationServicesEnabled())
{
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations.last as! CLLocation
let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
var latitude: Double = location.coordinate.latitude
var longitude: Double = location.coordinate.longitude
let initialLocation = CLLocation(latitude: latitude, longitude: longitude)
// Section 1
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "Coffee"
// Section 2
let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
request.region = MKCoordinateRegion(center: initialLocation.coordinate, span: span)
// Section 3
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler {
(response: MKLocalSearchResponse!, error: NSError!) in
for item in response.mapItems as! [MKMapItem] {
println(item.name)
//println("Latitude = \(item.placemark.location.coordinate.latitude)")
//println("Longitude = \(item.placemark.location.coordinate.longitude)")
self.addPinToMapView(item.name, latitude: item.placemark.location.coordinate.latitude, longitude: item.placemark.location.coordinate.longitude)
}
}
locationManager.stopUpdatingLocation()
let coordinateRegion = MKCoordinateRegionMakeWithDistance(initialLocation.coordinate, searchRadius * 2.0, searchRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
func addPinToMapView(title: String, latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let annotation = MyAnnotation(coordinate: location, title: title)
mapView.addAnnotation(annotation)
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
println("Error: " + error.localizedDescription)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment