iOS: 如何根据 CoreLocation 坐标设置 GMSCoordinateBounds?
iOS: How to set GMSCoordinateBounds from CoreLocation Coordinates?
我正在构建的应用程序要求我询问用户他的位置,自动完成,并确保他确实在附近。所以我的想法是使用Google Places Autocomplete,return UITableView 中的结果。
为了确保用户在他打字的地方附近,我想将 GMSCoordinatesBound 设置为用户当前位置周围的特定半径。我知道如何通过 CoreLocation 获取用户的位置,但是我不确定如何将这些坐标转换为我可以在 Google Places autocompleteQuery 中使用的 GMSCoordinateBounds 对象。有人可以给我提示吗?
谢谢!
首先,澄清一下可能发生的情况:
- 您为自动完成提供的
bounds
会强烈偏向该区域或附近的结果,但这不是硬性限制。用户仍然可以输入 埃菲尔铁塔、法郎 和 select 法国巴黎的埃菲尔铁塔,即使位置在新西兰。
- 自动完成边界是一个矩形,而不是一个圆,所以为了迂腐你不能偏向 半径。但是角与中心有一定距离的矩形就可以了。
您可以将您的坐标偏移固定距离,为东北角和西南角创建新坐标,例如通过使用 (对 "Moving a CLLocation by x meters" 的回答)。
然后您可以从 CLLocationCoordinate2D
构造一个 GMSCoordinateBounds
,其中:
GMSCoordinateBounds *bounds =
[[GMSCoordinateBounds alloc] initWithCoordinate:northEast
coordinate:southWest];
然后将其传递给 autocompleteQuery
。
您现在可以限制 Google 自动完成 ViewController 搜索的范围。看这里:https://developers.google.com/places/ios-api/autocomplete#set_the_gmscoordinatebounds_of_autocomplete
具体来说,您可以使用 CLLocationManager 获取经纬度。然后添加偏移量以获得地图区域的边界区域。
您可以在视图中没有地图的情况下执行此操作。例如,对于向字段添加地址的情况。
使用https://developers.google.com/places/ios-api/autocomplete#add_a_full-screen_control设置全屏搜索。但是在激活搜索自动完成的 IBAction 中添加 (Swift 2) 此代码:
let manager = CLLocationManager()
manager.startUpdatingLocation()
let lat = manager.location!.coordinate.latitude
let long = manager.location!.coordinate.longitude
let offset = 200.0 / 1000.0;
let latMax = lat + offset;
let latMin = lat - offset;
let lngOffset = offset * cos(lat * M_PI / 200.0);
let lngMax = long + lngOffset;
let lngMin = long - lngOffset;
let initialLocation = CLLocationCoordinate2D(latitude: latMax, longitude: lngMax)
let otherLocation = CLLocationCoordinate2D(latitude: latMin, longitude: lngMin)
let bounds = GMSCoordinateBounds(coordinate: initialLocation, coordinate: otherLocation)
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.autocompleteBounds = bounds
autocompleteController.delegate = self
self.presentViewController(autocompleteController, animated: true, completion: nil)
我在 Google 文档和这个 post 的帮助下解决了这个问题:
Moving a CLLocation by x meters
根据 post 调整参数以更改边界区域。现在我得到本地搜索结果,而不是默认的纽约及其他地区。
这假设您已正确设置 iOS 的 Google 个地点 API。并且,您的 Google iOS API 安装密钥(在 AppDelegate 中),安装 Google 客户端和 ViewController 中的 AutoCompleteViewControllerDelegate 扩展.
我的两分钱。
extension GMSCoordinateBounds {
convenience init(location: CLLocationCoordinate2D, radiusMeters: CLLocationDistance) {
let region = MKCoordinateRegionMakeWithDistance(location, radiusMeters, radiusMeters)
self.init(coordinate: region.northWest, coordinate: region.southEast)
}
}
extension MKCoordinateRegion {
var northWest: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: center.latitude + span.latitudeDelta / 2, longitude: center.longitude - span.longitudeDelta / 2)
}
var southEast: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: center.latitude - span.latitudeDelta / 2, longitude: center.longitude + span.longitudeDelta / 2)
}
}
正在使用
let bounds = GMSCoordinateBounds(location: coordinate, radiusMeters: 20000)
我正在构建的应用程序要求我询问用户他的位置,自动完成,并确保他确实在附近。所以我的想法是使用Google Places Autocomplete,return UITableView 中的结果。
为了确保用户在他打字的地方附近,我想将 GMSCoordinatesBound 设置为用户当前位置周围的特定半径。我知道如何通过 CoreLocation 获取用户的位置,但是我不确定如何将这些坐标转换为我可以在 Google Places autocompleteQuery 中使用的 GMSCoordinateBounds 对象。有人可以给我提示吗?
谢谢!
首先,澄清一下可能发生的情况:
- 您为自动完成提供的
bounds
会强烈偏向该区域或附近的结果,但这不是硬性限制。用户仍然可以输入 埃菲尔铁塔、法郎 和 select 法国巴黎的埃菲尔铁塔,即使位置在新西兰。 - 自动完成边界是一个矩形,而不是一个圆,所以为了迂腐你不能偏向 半径。但是角与中心有一定距离的矩形就可以了。
您可以将您的坐标偏移固定距离,为东北角和西南角创建新坐标,例如通过使用 (对 "Moving a CLLocation by x meters" 的回答)。
然后您可以从 CLLocationCoordinate2D
构造一个 GMSCoordinateBounds
,其中:
GMSCoordinateBounds *bounds =
[[GMSCoordinateBounds alloc] initWithCoordinate:northEast
coordinate:southWest];
然后将其传递给 autocompleteQuery
。
您现在可以限制 Google 自动完成 ViewController 搜索的范围。看这里:https://developers.google.com/places/ios-api/autocomplete#set_the_gmscoordinatebounds_of_autocomplete
具体来说,您可以使用 CLLocationManager 获取经纬度。然后添加偏移量以获得地图区域的边界区域。
您可以在视图中没有地图的情况下执行此操作。例如,对于向字段添加地址的情况。
使用https://developers.google.com/places/ios-api/autocomplete#add_a_full-screen_control设置全屏搜索。但是在激活搜索自动完成的 IBAction 中添加 (Swift 2) 此代码:
let manager = CLLocationManager()
manager.startUpdatingLocation()
let lat = manager.location!.coordinate.latitude
let long = manager.location!.coordinate.longitude
let offset = 200.0 / 1000.0;
let latMax = lat + offset;
let latMin = lat - offset;
let lngOffset = offset * cos(lat * M_PI / 200.0);
let lngMax = long + lngOffset;
let lngMin = long - lngOffset;
let initialLocation = CLLocationCoordinate2D(latitude: latMax, longitude: lngMax)
let otherLocation = CLLocationCoordinate2D(latitude: latMin, longitude: lngMin)
let bounds = GMSCoordinateBounds(coordinate: initialLocation, coordinate: otherLocation)
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.autocompleteBounds = bounds
autocompleteController.delegate = self
self.presentViewController(autocompleteController, animated: true, completion: nil)
我在 Google 文档和这个 post 的帮助下解决了这个问题: Moving a CLLocation by x meters
根据 post 调整参数以更改边界区域。现在我得到本地搜索结果,而不是默认的纽约及其他地区。
这假设您已正确设置 iOS 的 Google 个地点 API。并且,您的 Google iOS API 安装密钥(在 AppDelegate 中),安装 Google 客户端和 ViewController 中的 AutoCompleteViewControllerDelegate 扩展.
我的两分钱。
extension GMSCoordinateBounds {
convenience init(location: CLLocationCoordinate2D, radiusMeters: CLLocationDistance) {
let region = MKCoordinateRegionMakeWithDistance(location, radiusMeters, radiusMeters)
self.init(coordinate: region.northWest, coordinate: region.southEast)
}
}
extension MKCoordinateRegion {
var northWest: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: center.latitude + span.latitudeDelta / 2, longitude: center.longitude - span.longitudeDelta / 2)
}
var southEast: CLLocationCoordinate2D {
return CLLocationCoordinate2D(latitude: center.latitude - span.latitudeDelta / 2, longitude: center.longitude + span.longitudeDelta / 2)
}
}
正在使用
let bounds = GMSCoordinateBounds(location: coordinate, radiusMeters: 20000)