如何在MKMapView中使用MKLocalSearch [英] How to use MKLocalSearch In MKMapView

查看:109
本文介绍了如何在MKMapView中使用MKLocalSearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MKMapView中使用MKLocalSearch.我正在实现以下目标

i am using MKLocalSearch in MKMapView..I am implementing as follow

extension MYClass: SendLocationDelegate{

    func sendCoOrdinates(loccoordinate:CLLocation, placemark:CLPlacemark){

        println(" Google VC coordinate is as \(loccoordinate.coordinate.longitude) \(loccoordinate.coordinate.latitude)")
        let location:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude:loccoordinate.coordinate.latitude, longitude: loccoordinate.coordinate.longitude)
        let theSpan : MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta:0.01)
        let theRegion : MKCoordinateRegion = MKCoordinateRegion(center: location, span: theSpan)
        self.mapView.setRegion(theRegion, animated: false)

        let request = MKLocalSearchRequest()
        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")

                println("\(response)")

            }
        })



    }


}

输出:Google VC坐标为72.8561644 19.0176147 搜索发生错误:操作无法完成. (MKErrorDomain错误1.)为什么会发生这种情况?

Output: Google VC coordinate is as 72.8561644 19.0176147 Error occured in search: The operation couldn’t be completed. (MKErrorDomain error 1.) Why is this happening?

但是,当我将请求更改为

EDITED: however when i change request as

let request = MKLocalSearchRequest()
 request.naturalLanguageQuery = "india"
 //request.region = mapView.region
 let search = MKLocalSearch(request: request)

我得到了很好的答复

{ boundingRegion ="; mapItems =( "{\ n isCurrentLocation = 0; \ n名称=印度; \ n地标= \"印度印度@ < + 23.04117260,+ 78.89180550> +/- 0.00m,区域CLCircularRegion (标识符:'< + 21.84329084,+ 82.78786665>半径2237301.34', 中心:< + 21.84329084,+ 82.78786665>,半径:2237301.34m)\; \ n}" ); }

{ boundingRegion = ""; mapItems = ( " {\n isCurrentLocation = 0;\n name = India;\n placemark = \"India, India @ <+23.04117260,+78.89180550> +/- 0.00m, region CLCircularRegion (identifier:'<+21.84329084,+82.78786665> radius 2237301.34', center:<+21.84329084,+82.78786665>, radius:2237301.34m)\";\n}" ); }

推荐答案

一切正常..您只是在请求时犯了一个错误,该请求中不包含naturalLanguageQuery作为

Everything is working fine..Just you did a mistake on request that doesnot contains a naturalLanguageQuery as

 let request = MKLocalSearchRequest()
 request.region = mapView.region
 let search = MKLocalSearch(request: request)

在这种情况下,将request.naturalLanguageQuery设置为nil ....所以您得到了未知类型的(MKErrorDomain错误1).请查看Apple文档

In this case request.naturalLanguageQuery is set it to nil....so you got (MKErrorDomain error 1) of unknown type.Look on Apple docs herenaturalLanguageQuery cannot contain nil value...So make a request with it

let request = MKLocalSearchRequest()
 request.naturalLanguageQuery = "india"
 let search = MKLocalSearch(request: request)

您可以使用region参数将搜索结果列表缩小到指定区域内或附近.指定区域并不能保证结果都将在该区域内.这只是对搜索引擎的提示.因此,region可以在此处充当可选项.或者,您也可以请求

You can use region parameter to narrow the list of search results to those inside or close to the specified region. Specifying a region does not guarantee that the results will all be inside the region. It is merely a hint to the search engine. So region can act as an optional here. Or you can make request as for better results as

let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "india"
request.region = mapView.region
let search = MKLocalSearch(request: request)

这篇关于如何在MKMapView中使用MKLocalSearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆