如何在搜索位置时确定字符串是自然语言还是地址?

时间:2015-11-17 21:13:44

标签: swift

我正在尝试实施像搜索栏这样的苹果地图,以便用户能够找到特定的位置或地址,并在搜索到的位置保存图钉。现在,我理解如何获取自然语言的地址以及如何从地址获取坐标。但我不知道如何有效地解析字符串并知道要做出哪个请求。我无法在网上找到任何内容,只有电子邮件地址的文章。任何突出标准/良好实施的信息都将非常感谢!

2 个答案:

答案 0 :(得分:1)

您可以使用 <div class="contact hidden-xs"> <span>Call Now! <a href="tel:2058708787">(205) 870-8787</a></span> <br /> <a href="https://www.facebook.com/Cocke-Chiropractic-Clinic-122171807867845/?fref=ts" target="_blank"><img src="<?php bloginfo('template_directory'); ?>/images/fbsmall.png" class="social img-responsive pull-right" /></a> </div> 检查字符串是否为某个位置。您可以像这样格式化代码:

CLGeocoder

但是,如果在大型阵列上使用,这可能会非常密集。但对搜索栏来说应该没问题!

答案 1 :(得分:1)

感谢Maclean和Location Manager by Varshyl Mobile。注意:这是我能够想出的答案,解决了我的问题;仍然有问题,让弹出正确的自然语言位置。

let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address as String, completionHandler: {(placemarks: [CLPlacemark]?, error: NSError?) -> Void in

        if (error != nil) {
            print("error")
        }
        else{
            if let placemark = placemarks?.first {
                let address = AddressParser()
                address.parseAppleLocationData(placemark)
                let addressDict = address.getAddressDictionary()
                print("searched address")
                print(addressDict)
            }
            else {
                print("invalid address")
            }
        }

    })

以下是我从上面提到的库中使用的类/函数。

private class AddressParser: NSObject{

 private var latitude = NSString()
 private var longitude  = NSString()
 private var streetNumber = NSString()
 private var route = NSString()
 private var locality = NSString()
 private var subLocality = NSString()
 private var formattedAddress = NSString()
 private var administrativeArea = NSString()
 private var administrativeAreaCode = NSString()
 private var subAdministrativeArea = NSString()
 private var postalCode = NSString()
 private var country = NSString()
 private var subThoroughfare = NSString()
 private var thoroughfare = NSString()
 private var ISOcountryCode = NSString()
 private var state = NSString()


override init(){

    super.init()

}

private func getAddressDictionary()-> NSDictionary{

    let addressDict = NSMutableDictionary()

    addressDict.setValue(latitude, forKey: "latitude")
    addressDict.setValue(longitude, forKey: "longitude")
    addressDict.setValue(streetNumber, forKey: "streetNumber")
    addressDict.setValue(locality, forKey: "locality")
    addressDict.setValue(subLocality, forKey: "subLocality")
    addressDict.setValue(administrativeArea, forKey: "administrativeArea")
    addressDict.setValue(postalCode, forKey: "postalCode")
    addressDict.setValue(country, forKey: "country")
    addressDict.setValue(formattedAddress, forKey: "formattedAddress")

    return addressDict
}


private func parseAppleLocationData(placemark:CLPlacemark){

    let addressLines = placemark.addressDictionary!["FormattedAddressLines"] as! NSArray

    //self.streetNumber = placemark.subThoroughfare ? placemark.subThoroughfare : ""
    self.streetNumber = (placemark.thoroughfare != nil ? placemark.thoroughfare : "")!
    self.locality = (placemark.locality != nil ? placemark.locality : "")!
    self.postalCode = (placemark.postalCode != nil ? placemark.postalCode : "")!
    self.subLocality = (placemark.subLocality != nil ? placemark.subLocality : "")!
    self.administrativeArea = (placemark.administrativeArea != nil ? placemark.administrativeArea : "")!
    self.country = (placemark.country != nil ?  placemark.country : "")!
    self.longitude = placemark.location!.coordinate.longitude.description;
    self.latitude = placemark.location!.coordinate.latitude.description
    if(addressLines.count>0){
        self.formattedAddress = addressLines.componentsJoinedByString(", ")}
    else{
        self.formattedAddress = ""
    }


}