Automatic business search Swift Mapkit

I am trying to create an automatic search in my application using Mapkit. I would like the viewcontroller to find the current location and all the hotels in their area. I managed to get the current location, but I cannot find any automatic search tutorials from the string. This is what I still have:

import UIKit
import MapKit
import CoreLocation

class MapClass: UIViewController, CLLocationManagerDelegate, UISearchBarDelegate {
    @IBOutlet weak var mapView: MKMapView!
    var locationManager: CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()

        if (CLLocationManager.locationServicesEnabled())
        {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
            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)

        locationManager.stopUpdatingLocation()
    }
+4
source share
1 answer

Thanks to Nate Cook pointing me in the right direction, I was able to solve the problem with the following code:

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 = 2000

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)
    // 1
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = "Restaurant"
    // 2
    let span = MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)

    request.region = MKCoordinateRegion(center: initialLocation.coordinate, span: span)
    // 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()
}
}
+3
source

All Articles