谷歌地图在swift中获取街道编号



如何在swift中从谷歌地图中获取街道编号?

我的代码:

let geocoder = GMSGeocoder()
geocoder.reverseGeocodeCoordinate(place.coordinate) { response, error in
   if let address = response?.firstResult() {
      print(address)
   }
})

我得到这个:

GMSAddress {
coordinate: (56.966157, 24.054405)
lines: Buļļu iela 21, Kurzeme District, Rīga, LV-1055, Latvia
thoroughfare: 21 Buļļu iela
locality: Rīga
administrativeArea: Rīgas pilsēta
postalCode: LV-1055
country: Latvia
}

所以。。。如何获取街道号码?

使用正则表达式,您可以一次性从thoroughfare中获得街道编号和街道。这使您的地址解析代码更加灵活,因为如果您决定稍后对splitStreetNumber(in:)方法进行调整,您只需对其进行最小的更改。此外,子字符串方法是非常可重用的。

extension NSRegularExpression {
    func substrings(in string: String, options: MatchingOptions = [], range rangeOrNil: NSRange? = nil) -> [[String?]] {
        // search the full range of the string by default
        let range = rangeOrNil ?? NSRange(location: 0, length: string.count)
        let matches = self.matches(in: string, options: options, range: range)
        // parse each match's ranges into strings
        return matches.map{ match -> [String?] in
            return (0..<match.numberOfRanges)
                // map each index to a range
                .map{ match.range(at: $0) }
                // map each range to a substring of the given string if the range is valid
                .map{ matchRange -> String? in
                    if let validRange = range.intersection(matchRange) {
                        return (string as NSString).substring(with: validRange)
                    }
                    return nil
            }
        }
    }
}
func splitStreetNumber(in streetAddress: String) -> (streetNumber: String?, street: String?) {
    // regex matches digits at the beginning and any text after without capturing any extra spaces
    let regex = try! NSRegularExpression(pattern: "^ *(\d+)? *(.*?) *$", options: .caseInsensitive)
    // because the regex is anchored to the beginning and end of the string there will
    // only ever be one match unless the string does not match the regex at all
    if let strings = regex.substrings(in: streetAddress).first, strings.count > 2 {
        return (strings[1], strings[2])
    }
    return (nil, nil)
}

结果

'  1234 Address Dr  ' -> ('1234', 'Address Dr')
'1234 Address Dr'     -> ('1234', 'Address Dr')
'Address Dr'          -> (nil, 'Address Dr')
'1234'                -> ('1234', nil)

thoroughfare返回街道和街道编号。查看GMSAddress文档。

如果您想从thoroughfare结果string中获得街道编号,可以使用以下方法。

func getStreetNumber(street : String){
     let str = street
     let streetNumber = str.componentsSeparatedByCharactersInSet(
     NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
     print(streetNumber)
}

更新

只获取号码

我已经调用了getStreetNumber("531 W 217th St, New York"),下面的方法打印出531

var number = ""
var hasValue = false
// Loops thorugh the street
for i in street.characters {
    let str = String(i)
    // Checks if the char is a number
    if (Int(str) != nil){
        // If it is it appends it to number
        number+=str
       // Here we set the hasValue to true, beacause the street number will come in one order
       // 531 in this case
       hasValue = true
    }
    else{
        // Lets say that we have runned through 531 and are at the blank char now, that means we have looped through the street number and can end the for iteration
        if(hasValue){
            break
        }
   }
}
print(number)

仅获取地址

func getStreetNumber(street : String){
        let str = street
        let streetNumber = str.componentsSeparatedByCharactersInSet(
            NSCharacterSet.decimalDigitCharacterSet().invertedSet).joinWithSeparator("")
        print(streetNumber)
        var address = street
        var hasValue = false
        // Loops thorugh the street
        for i in street.characters {
            let str = String(i)
            // Checks if the char is a number
            if (Int(str) != nil){
                // If it is it appends it to number
                address = address.stringByReplacingOccurrencesOfString(str, withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
                // Here we set the hasValue to true, beacause the street number will come in one order
                // 531 in this case
                hasValue = true
            }
            else{
                // Lets say that we have runned through 531 and are at the blank char now, that means we have looped through the street number and can end the for iteration
                if(hasValue){
                    break
                }
            }
        }
        print(address)
    }

它对我来说很好:

func splitStreetNumber(in streetAddress: String) -> (streetNumber: String?, street: String?) {
    let words = streetAddress.split(separator: " ")
    var lastIndex: Int?
    for (index, word) in words.enumerated() where word.rangeOfCharacter(from: .decimalDigits) != nil {
        lastIndex = index
    }
    guard let lindex = lastIndex else { return (nil, streetAddress) }
    var nChars: [String] = []
    var sChars: [String] = []
    for (index, word) in words.enumerated() {
        if index <= lindex {
            nChars.append(String(word))
        } else {
            sChars.append(String(word))
        }
    }
    let number = nChars.joined(separator: " ")
    let street = sChars.joined(separator: " ")
    return (number.isEmpty ? nil : number, street.isEmpty ? nil : street)
}

最新更新