从地址获取坐标的完成处理程序
Completion handler for getting coordinates from address
我很难理解完成处理程序。而且我不知道如何使用它。
这是带有完成处理程序的函数,用于根据字符串地址获取地理坐标:
func getLocation(from address: String, completion: @escaping (_ location: CLLocation?)-> Void) {
guard let address = address as? String else { return }
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, error) in
guard let placemarks = placemarks,
let location = placemarks.first?.location else {
completion(nil)
return
}
completion(location)
}
}
我是这样调用这个函数的:
getLocation(from: address) { location in
print("Location is", location.debugDescription)
if case self.currentLocation = location {
self.queryGenerator(searched: self.searchController.isActive, queryString: "")
} else {
self.showAlert(alertTitle: "No businsesses nearby", message: "Try going back and changing the address")
}
}
currentLocation
我总是得不到零。如果有人能为我把它简化一下,那就太好了。
我在获取位置后进行了网络调用,该位置向我发送了一个包含企业坐标和服务半径的自定义对象。如果服务半径小于或等于业务坐标和 currentLocation 之间的距离,那么我将它添加到填充 UITableView 的数组中。这是我进行上述计算的函数:
func applicableRestaurants(allQueriedRestaurants: [Restaurant]) -> [Restaurant] {
var filteredRestaurants = [Restaurant]()
for thisRestaurant in allQueriedRestaurants {
let restaurantCoordinate = CLLocation(latitude: thisRestaurant.geoPoint.latitude, longitude: thisRestaurant.geoPoint.longitude)
if restaurantCoordinate.distance(from: self.currentLocation!) <= thisRestaurant.distance {
filteredRestaurants.append(thisRestaurant)
}
}
return filteredRestaurants
}
我的猜测(这只是一个猜测)是您在不理解其含义的情况下使用了这个表达式:
if case self.currentLocation = location {
self.queryGenerator...
我认为你的意思可能是:
if let location = location {
self.currentLocation = location
self.queryGenerator...
我想你可能认为它们是等价的,但它们不是。
我很难理解完成处理程序。而且我不知道如何使用它。
这是带有完成处理程序的函数,用于根据字符串地址获取地理坐标:
func getLocation(from address: String, completion: @escaping (_ location: CLLocation?)-> Void) {
guard let address = address as? String else { return }
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) { (placemarks, error) in
guard let placemarks = placemarks,
let location = placemarks.first?.location else {
completion(nil)
return
}
completion(location)
}
}
我是这样调用这个函数的:
getLocation(from: address) { location in
print("Location is", location.debugDescription)
if case self.currentLocation = location {
self.queryGenerator(searched: self.searchController.isActive, queryString: "")
} else {
self.showAlert(alertTitle: "No businsesses nearby", message: "Try going back and changing the address")
}
}
currentLocation
我总是得不到零。如果有人能为我把它简化一下,那就太好了。
我在获取位置后进行了网络调用,该位置向我发送了一个包含企业坐标和服务半径的自定义对象。如果服务半径小于或等于业务坐标和 currentLocation 之间的距离,那么我将它添加到填充 UITableView 的数组中。这是我进行上述计算的函数:
func applicableRestaurants(allQueriedRestaurants: [Restaurant]) -> [Restaurant] {
var filteredRestaurants = [Restaurant]()
for thisRestaurant in allQueriedRestaurants {
let restaurantCoordinate = CLLocation(latitude: thisRestaurant.geoPoint.latitude, longitude: thisRestaurant.geoPoint.longitude)
if restaurantCoordinate.distance(from: self.currentLocation!) <= thisRestaurant.distance {
filteredRestaurants.append(thisRestaurant)
}
}
return filteredRestaurants
}
我的猜测(这只是一个猜测)是您在不理解其含义的情况下使用了这个表达式:
if case self.currentLocation = location {
self.queryGenerator...
我认为你的意思可能是:
if let location = location {
self.currentLocation = location
self.queryGenerator...
我想你可能认为它们是等价的,但它们不是。