ios - Swift 是否可以为 MKLocalSearchRequest 和 MKLocalSearchResponse 设置一个特定的国家

标签 ios objective-c iphone xcode swift

我想知道是否可以在 mapKit 中为 MKLocalSearchRequest 和 MKLocalSearchResponse 设置一个国家?

在我的 map View 中,用户可以搜索 zip /州。但我不想在其他国家/地区展示点击量,因为它在这里没有任何意义。

提前致谢

localSearchRequest = MKLocalSearchRequest()
        localSearchRequest.naturalLanguageQuery = "23138"
        localSearch = MKLocalSearch(request: localSearchRequest)
        localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in



            if localSearchResponse == nil || self.mapView == nil{
                var alert = UIAlertView(title: nil, message: "Place not found", delegate: self, cancelButtonTitle: "Try again")
                alert.show()
                return
            } else {

                let location = CLLocationCoordinate2D(latitude: localSearchResponse.boundingRegion.center.latitude, longitude:     localSearchResponse.boundingRegion.center.longitude)


                let span = MKCoordinateSpanMake(0.05, 0.05)
                let region = MKCoordinateRegion(center: location, span: span)
                self.mapView.setRegion(region, animated: false)


                self.pointAnnotation = MKPointAnnotation()
                self.pointAnnotation.coordinate = location
                self.mapView.addAnnotation(self.pointAnnotation)
            }


        }

最佳答案

虽然似乎没有办法明确限制 MKLocalSearchRequest对于特定的地理环境,您可以通过以下步骤非常接近:

  1. 设置搜索请求的region MKCoordinateRegion 的属性(property)对应于美国(不幸的是,阿拉斯加和夏威夷使该区域相当大;根据您的应用,您可能只想使用美国大陆)。
  2. 在您的回调处理程序中,您可以过滤 MKMapItemMKLocalSearchResponse 返回的数组通过他们 placemark属性' countryCode属性,仅使用国家代码为 "US" 的那些.

如果您没有得到想要的结果,您可以调查其他第三方 API 服务,看看其中是否更适合您的特定用例。

关于ios - Swift 是否可以为 MKLocalSearchRequest 和 MKLocalSearchResponse 设置一个特定的国家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31661565/

相关文章:

ios - 任何手指扫描示例教程iPhone SDK

ios - 如何遍历 NSDictionaries 和 NSArrays 的嵌套层次结构并将其全部转换为可变副本?

iphone - Cocos2d - animationwithframes :delay: deprecated

ios - 程序化自动布局 UI 元素

objective-c - 字典范围?

ios - 如何从iOS中的位置数组中找到5m到10m范围内的最近位置?

objective-c - NSManagedObjectContextDidSaveNotification 更新 UI 的最佳方法?

ios - Scenekit - 我如何拖放

ios - 在 XMPP 中以后台模式工作

ios - 使用 ios 更新用户配置文件?