iOS:如何从CoreLocation坐标设置GMSCoordinateBounds?

Cha*_*nel 4 autocomplete core-location uitableview ios google-places-api

我正在构建的应用程序要求我询问用户他的位置,自动完成,并确保他确实在附近.所以我的想法是使用Google Places Autocomplete,并将结果返回到UITableView.

为了确保用户位于他正在键入的位置附近,我想将GMSCoordinatesBound设置为用户当前位置周围的特定半径.我知道如何通过CoreLocation获取用户的位置,但我不知道如何将这些坐标转换为我可以在Google Places autocompleteQuery中使用的GMSCoordinateBounds对象.任何人都可以给我一个提示吗?

谢谢!

D. *_*ild 8

您现在可以限制Google Autocomplete ViewController搜索的范围.请参阅此处:https://developers.google.com/places/ios-api/autocomplete#set_the_gmscoordinatebounds_of_autocomplete

具体来说,您可以使用CLLocationManager来获取lat和long.然后添加偏移量以获取地图区域的边界区域.

您可以在视图中没有地图的情况下执行此操作.例如,对于向字段添加地址的情况.

使用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)
Run Code Online (Sandbox Code Playgroud)

我在谷歌文档和这篇文章的帮助下想出了这一点: 将一个CLLocation移动x米

调整帖子的参数以更改边界区域.现在我获得了本地搜索结果,而不是默认的纽约及其他.

这假设您已正确设置适用于iOS的Google Places API.并且,您安装了Google iOS API密钥(在AppDelegate中),安装了Google客户端以及ViewController中的AutoCompleteViewControllerDelegate扩展.


Ars*_*nik 7

我的两分钱。

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

使用

let bounds = GMSCoordinateBounds(location: coordinate, radiusMeters: 20000)
Run Code Online (Sandbox Code Playgroud)


spi*_*piv 2

首先,对可能发生的事情进行一些澄清:

  1. bounds给予自动完成功能的设置将使结果强烈偏向于该区域内或附近,但这不是硬性限制。即使位置是新西兰,用户仍然可以输入埃菲尔铁塔、法郎并选择法国巴黎的埃菲尔铁塔。
  2. 自动完成边界是一个矩形,而不是一个圆形,所以为了迂腐,你不能偏向半径。但角点距中心一定距离的矩形就可以了。

您可以将坐标偏移固定距离,为东北角和西南角创建新坐标,例如使用/sf/answers/509480541/(回答“将 CLLocation 移动 x 米”)。

GMSCoordinateBounds然后,您可以使用以下内容从 a构造 a CLLocationCoordinate2D

GMSCoordinateBounds *bounds = 
  [[GMSCoordinateBounds alloc] initWithCoordinate:northEast
                                       coordinate:southWest];
Run Code Online (Sandbox Code Playgroud)

然后将其传递给autocompleteQuery.