Swift Mapkit自动商业搜索

时间:2015-06-23 00:57:36

标签: ios swift mapkit

我正在尝试使用Mapkit在我的应用内创建自动搜索。我想viewcontroller找到他们所在地区的当前位置和所有酒店。 我已经能够获得当前位置,但我找不到任何关于从字符串进行自动搜索的教程。这就是我到目前为止所做的:

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()
    }

1 个答案:

答案 0 :(得分:4)

感谢Nate Cook指出我正确的方向,我能够通过以下代码解决问题:

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()
}
}