Swift Local Search MapView

时间:2015-02-17 01:56:44

标签: ios swift mapkit

我目前正在将本地搜索集成到我的iOS应用中,而且我遇到了一些问题。一切都很好,没有错误,但搜索功能没有发生。我已经在模拟器和iOS设备上运行了它,而且功能似乎没有运行。我检查了我的控制台日志,但没有任何事情发生。

italic *编辑:我已解决了未调用方法的问题,但我现在收到以下错误。如果需要,我可以粘贴整个调用堆栈

"searchText:]: unrecognized selector sent to instance 0x7ff2f1c1c3b0 2015-02-16 21:10:26.818 THINKStatus[60477:1795955] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[THINKStatus.loggedinViewController searchText:]: unrecognized selector sent to instance 0x7ff2f1c1c3b0'"

以下是我的视图控制器的代码:

import Foundation
import UIKit
import MapKit

class loggedinViewController : UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var searchText: UITextField!
var matchingItems: [MKMapItem] = [MKMapItem]()

let service = "tgsLogin"
let userAccount = "tgsLoginUser"
let key = "RandomKey"


@IBAction func loggedinActionButton(sender: AnyObject) {
    let error = Locksmith.deleteDataForUserAccount("tgsLoginUser", inService: "tgsLogin")

    self.performSegueWithIdentifier("logoutViewSegue", sender: self)

}

@IBAction func textFieldReturn(sender: AnyObject) {
    sender.resignFirstResponder()
    mapView.removeAnnotations(mapView.annotations)
    self.performSearch()
}

func performSearch() {

    matchingItems.removeAll()
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchText.text
    request.region = mapView.region

    let search = MKLocalSearch(request: request)

    search.startWithCompletionHandler({(response:
        MKLocalSearchResponse!,
        error: NSError!) in

        if error != nil {
            println("Error occured in search: \(error.localizedDescription)")
        } else if response.mapItems.count == 0 {
            println("No matches found")
        } else {
            println("Matches found")

            for item in response.mapItems as [MKMapItem] {
                println("Name = \(item.name)")
                println("Phone = \(item.phoneNumber)")

                self.matchingItems.append(item as MKMapItem)
                println("Matching items = \(self.matchingItems.count)")

                var annotation = MKPointAnnotation()
                annotation.coordinate = item.placemark.coordinate
                annotation.title = item.name
                self.mapView.addAnnotation(annotation)
            }
        }
    })
}
override func viewDidLoad() {
    super.viewDidLoad()
    mapView.showsUserLocation = true
    mapView.delegate = self


}


}

app delegate:

  class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var locationManager: CLLocationManager?


  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.
    locationManager = CLLocationManager()
    locationManager?.requestWhenInUseAuthorization()

    return true
}

2 个答案:

答案 0 :(得分:0)

错误消息表明您可能在某处使用searchText,就像它是一个函数一样。我在你的代码中没有看到,所以有几种可能性:

  1. 您还有一些其他代码使用了您未发布的代码
  2. 二进制文件与上面的源代码不同步
  3. 框架中有一个错误
  4. 我会专注于前两个。尝试查看所有错误消息,以查看代码中是否有任何行失败的迹象。尝试清理项目并重建。尝试在所有源文件中搜索“searchText”...

答案 1 :(得分:0)

这是适合我的代码。我看到的唯一区别是一些问号而不是感叹号。如果这对您有用,请告诉我。

@IBAction func textFieldReturn(sender: AnyObject) {

    sender.resignFirstResponder()
    mapView.removeAnnotations(mapView.annotations)
    self.performSearch()

}


func performSearch() {

    matchingItems.removeAll()
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchText.text
    request.region = mapView.region

    let search = MKLocalSearch(request: request)

    search.startWithCompletionHandler({
        (response: MKLocalSearchResponse?,error: NSError?) in

        if error != nil {
            print("Error occured in search: \(error!.localizedDescription)")
        } else if response!.mapItems.count == 0 {
            print("No matches found")
        } else {
            print("Matches found")

            for item in response!.mapItems {
                print("Name = \(item.name)")
                print("Phone = \(item.placemark)")

                self.matchingItems.append(item as MKMapItem)
                print("Matching items = \(self.matchingItems.count)")

                let annotation = MKPointAnnotation()
                annotation.coordinate = item.placemark.coordinate
                annotation.title = item.name
                annotation.subtitle = address
                self.mapAnnotations.append(annotation)
                self.mapView.addAnnotation(annotation)

            }
        }
    })
}