在GeoCoder函数Swift

时间:2019-02-27 16:39:03

标签: swift core-location cllocation

我有一个全局变量“ filterError”,该变量在switch语句之前最初设置为false。当switch语句出现时,它将通过一个字典,并且对于每个键,它将对值进行唯一的逻辑检查。如果该值不符合某些条件,则布尔变量filterError将设置为true。

当switch语句完成时,如果filterErrror为false,则将发生如下操作:

//...switch statement...

if (filterErrror == false) {
    //do something.....
}

我遇到的问题与地址解析器功能有关,在switch语句中就是这种情况。如果开关变量键=“ area”,则执行以下操作:

var geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, error) in

    guard let placemarks = placemarks,let location = placemarks.first?.location
    else { print("Error in finding location") return }
    let distanceInMeters = location.distance(from: coordinateSelf)
    print("The distance between property and user in miles is: \(distanceInMeters/1609)")
    distanceInMiles = distanceInMeters/1609
    var radius = Double()

    if searchRadius == "this area only" {
        radius = 0.49999999999
    } else {
        radius = Double(Int(searchRadius)!)
    }
    if (radius < distanceInMiles) {
        filterError = true
    }
}

但是,由于未更改布尔值,因此switch语句完成后filterError仍然为false。如何从地址解析器内部将filterError设置为true?

这是完整的代码:

for filter in self.activeFilters {
                                switch filter.key {
                                case "listingType":
                                    if(filter.value[0] != propertiesFirestore[i]["listingType"]![0]){
                                        filterError = true
                                    }
                                case "minBedsBound":
                                    if(filter.value[0] != "No min. beds") {
                                        if(filter.value[0].filter("01234567890.".contains) > propertiesFirestore[i]["bedroomCount"]![0]) {
                                            filterError = true
                                        }
                                    }
                                case "maxBedsBound":
                                    if(filter.value[0] != "No max. beds") {
                                        if(Int(filter.value[0].filter("01234567890.".contains))! < Int(propertiesFirestore[i]["bedroomCount"]![0])!) {
                                            filterError = true
                                        }
                                    }
                                case "minPriceBound":
                                    if(filter.value[0] != "No min. price") {
                                        if(Int(filter.value[0].filter("01234567890.".contains))! > Int(propertiesFirestore[i]["price"]![0])!) {
                                            filterError = true
                                        }
                                    }
                                case "maxPriceBound":
                                    if(filter.value[0] != "No max. price") {
                                        if(Int(filter.value[0].filter("01234567890.".contains))! < Int(propertiesFirestore[i]["price"]![0])!) {
                                            filterError = true
                                        }
                                    }
                                case "includeSharedOwnership":
                                    if(filter.value[0] != propertiesFirestore[i]["sharedOwnership"]![0]) {
                                        filterError = true
                                    }
                                case "includeRetirementHomes":
                                    if(filter.value[0] != propertiesFirestore[i]["retirementHome"]![0]) {
                                        filterError = true
                                    }
                                case "area":
                                    print(userlatitude)
                                    print(userlongitude)

                                    let address = "\(propertiesFirestore[i]["area"]![0]), \(propertiesFirestore[i]["postcodePrefix"]![0])"
                                    var propLat = String()
                                    var propLong = String()
                                    var distanceInMiles = Double()
                                    var searchRadius = self.activeFilters["searchRadius"]![0]
                                    let coordinateSelf = CLLocation(latitude: Double(userlatitude) as! CLLocationDegrees, longitude: Double(userlongitude) as! CLLocationDegrees)

                                    var geocoder = CLGeocoder()
                                    geocoder.geocodeAddressString(address) { (placemarks, error) in
                                        guard
                                            let placemarks = placemarks,
                                            let location = placemarks.first?.location
                                            else {
                                                print("Error in finding location")
                                                return
                                        }
                                        let distanceInMeters = location.distance(from: coordinateSelf)
                                        print("The distance between property and user in miles is: \(distanceInMeters/1609)")
                                        distanceInMiles = distanceInMeters/1609
                                        var radius = Double()
                                        if searchRadius == "this area only" {
                                            radius = 0.49999999999
                                        } else {
                                            radius = Double(Int(searchRadius)!)
                                        }
                                        if (radius < distanceInMiles) {
                                            filterError = true
                                        }
                                    }
                                case "keywords":
                                    print("Filter keywords are: \(filter.value)")
                                    for j in 0..<propertiesFirestore[i]["keywords"]!.count {
                                        propertiesFirestore[i]["keywords"]![j] = propertiesFirestore[i]["keywords"]![j].lowercased()
                                    }
                                    for keyword in filter.value {
                                        if propertiesFirestore[i]["keywords"] == nil {
                                            filterError = true
                                        } else if ((propertiesFirestore[i]["keywords"]!.contains(keyword.lowercased()))) {
                                            print("found keyword")
                                        } else {
                                            filterError = true
                                        }
                                    }

                                default:
                                    print("Unknown filter: \(filter.key)")

                            }

                            }

                            if filterError == false {
                                filterProperties.append(propertiesFirestore[i])
                            } else {
                                print("FITLER CATCHOUT")
                            }

0 个答案:

没有答案