【问题标题】:Bias Google Places results to a specific region将 Google 地方信息结果偏向特定区域
【发布时间】:2017-02-09 02:31:28
【问题描述】:

我正在使用官方Google Documentation 来创建地址查找。

在 Android 版本中,我可以将结果偏向我所在的区域。

例如,输入“123 Main St”会先显示我所在城镇的 Main st,然后再显示其他结果。

在 iOS 中,我不知道该怎么做。我正在使用带有GMSAutocompleteViewController 委托的“添加全屏控件”。

有一个GMSCoordinateBounds,但该示例是针对地图的。我没有使用地图。

我已经修改过类似的东西,但无法让它工作。我什至走在正确的轨道上吗?

- (void) placeAutocomplete : (NSString*) location : Bounds:(GMSCoordinateBounds *)bounds{                                                           
[_placesClient autocompleteQuery:location
                          bounds:bounds
                          filter:filter
                        callback:^(NSArray *results, NSError *error) {
                          if (error != nil) {
                            NSLog(@"Autocomplete error %@", [error localizedDescription]);
                            return;
                          }

                          for (GMSAutocompletePrediction* result in results) {
                            NSLog(@"Result '%@' with placeID %@", result.attributedFullText.string, result.placeID);
                          }
                        }];
}

有什么帮助吗?

【问题讨论】:

    标签: ios objective-c google-places-api gmsautocomplete


    【解决方案1】:

    您可以直接在GMSAutocompleteViewController 上设置边界,例如:

    // Present the Autocomplete view controller when the button is pressed.
    @IBAction func autocompleteClicked(_ sender: UIButton) {
        let autocompleteController = GMSAutocompleteViewController()
        autocompleteController.delegate = self
    
        // Set bounds to inner-west Sydney Australia.
        let neBoundsCorner = CLLocationCoordinate2D(latitude: -33.843366,
                                                   longitude: 151.134002)
        let swBoundsCorner = CLLocationCoordinate2D(latitude: -33.875725,
                                                   longitude: 151.200349)
        let bounds = GMSCoordinateBounds(coordinate: neBoundsCorner,
                                         coordinate: swBoundsCorner)
    
        autocompleteController.bounds = bounds
    
        present(autocompleteController, animated: true, completion: nil)
    }
    

    【讨论】:

    • 谢谢,但是您如何计算边界框的纬度/经度?您是否正盯着您所在的中心并向外扩展 20 英里?
    • 我不知道上面的代码(来自 Google 开发网站的 copypasta),但如果你想计算一个 20 英里的边界框,SphericalUtil 中的方法会有所帮助(developers.google.com/maps/documentation/android-api/utility/… )
    【解决方案2】:

    根据您的位置创建一个bounds,并将其设置为GMSAutocompleteViewController的边界

    func presentAddressFinder(){
         let autocompleteController = GMSAutocompleteViewController()
            autocompleteController.delegate = self
            let bounds = GMSCoordinateBounds()
            autocompleteController.autocompleteBounds = bounds.includingCoordinate(currentLocation)
        present(autocompleteController, animated: true, completion: nil)
    }
    

    这里currentLocationCLLocationCoordinate2D!中的当前设备位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 2012-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      相关资源
      最近更新 更多