【问题标题】:iOS: How to set GMSCoordinateBounds from CoreLocation Coordinates?iOS:如何从 CoreLocation 坐标设置 GMSCoordinateBounds?
【发布时间】:2015-11-20 13:54:32
【问题描述】:

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

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

谢谢!

【问题讨论】:

    标签: ios uitableview autocomplete google-places-api core-location


    【解决方案1】:

    您现在可以限制 Google 自动完成视图控制器搜索的范围。见这里: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 Docs 和这篇文章的帮助下解决了这个问题: Moving a CLLocation by x meters

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

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

    【讨论】:

    • 请注意,这仍然是设置偏见,而不是限制(正如我在回答中解释的那样)。
    • 我将原始问题解释为需要偏见,而不是绝对限制。我认为更广泛的用户案例是偏见,正如你所说,在这个区域搜索......但不限于这个区域。这是关于 Stackoverflow 的非常少的信息(我可以找到),它提供了关于如何获得偏见而不是使用 API 文档中的默认值的建议。感谢您的审核。
    • autocompleteBounds 已被弃用。看来您现在需要使用带有位置偏差的 GMSAutocompleteFilter() 来获得相同的结果。使用上述相同的界限,然后尝试: let filter = GMSAutocompleteFilter() filter.type = .address filter.locationBias = bounds as? GMSPlaceLocationBias autocompleteController.autocompleteFilter = 过滤器
    【解决方案2】:

    我的两分钱。

    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)
    

    【讨论】:

    • 伟大的扩展保持它
    【解决方案3】:

    首先,澄清一下什么是可能的:

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

    您可以将您的坐标偏移一个固定距离,以便为东北角和西南角创建新坐标,例如通过使用https://stackoverflow.com/a/7278293/806600(回答“将 CLLocation 移动 x 米”)。

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

    GMSCoordinateBounds *bounds = 
      [[GMSCoordinateBounds alloc] initWithCoordinate:northEast
                                           coordinate:southWest];
    

    然后将其传递给autocompleteQuery

    【讨论】:

    • 感谢您的详细回复。我有一个后续问题:有没有办法完全限制该区域,因为边界只是一种偏见?
    • 不,没有办法完全限制该区域。 (Google Places API 网络服务允许您通过 &components=country:us 将自动填充结果限制在特定国家/地区,但 iOS 风格的 Google Places API 没有此功能。
    • 我 2015 年 9 月评论的一个小更新:iOS 风格现在也支持国家/地区过滤器:developers.google.com/places/ios-api/reference/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    相关资源
    最近更新 更多