首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >快速本地搜索MapView

快速本地搜索MapView
EN

Stack Overflow用户
提问于 2015-02-17 01:56:43
回答 2查看 1.3K关注 0票数 0

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

斜体*编辑:我已经修正了方法没有被调用的问题,但是现在我得到了下面的错误。如果需要,我可以粘贴整个调用堆栈。

"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'"

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

代码语言:javascript
复制
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


}


}

应用程序代表:

代码语言:javascript
复制
  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
}
EN

回答 2

Stack Overflow用户

发布于 2015-02-17 03:54:39

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

  1. 您有一些没有发布的使用它的其他代码。
  2. 二进制文件与上面的源代码不同步。
  3. 框架中有一个bug。

我会专注于前两件事。试着检查所有错误消息,看看代码中是否有任何失败的行指示。试着清理项目并进行重建。尝试在所有源文件中搜索"searchText“.

票数 0
EN

Stack Overflow用户

发布于 2016-01-27 00:02:57

这是对我有用的密码。我看到的唯一不同是一些问号,而不是感叹号。如果这对你有用的话请告诉我。

代码语言:javascript
复制
@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)

            }
        }
    })
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28553466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档