对于位置查找,谷歌自动完成工作很好,但它会返回世界各地的搜索结果。我想限制半径 1000 米或城市内的搜索结果。我点击了这个谷歌链接来集成自动完成搜索。项目中仅使用以下代码
-(void)autoComplete
{
GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
acController.delegate = self;
[self presentViewController:acController animated:YES completion:nil];
}
// Handle the user's selection.
- (void)viewController:(GMSAutocompleteViewController *)viewController
didAutocompleteWithPlace:(GMSPlace *)place {
[self dismissViewControllerAnimated:YES completion:nil];
// Do something with the selected place.
NSLog(@"Place name %@", place.name);
NSLog(@"Place address %@", place.formattedAddress);
NSLog(@"Place attributions %@", place.attributions.string);
}
- (void)viewController:(GMSAutocompleteViewController *)viewController
didAutocompleteWithError:(NSError *)error {
[self dismissViewControllerAnimated:YES completion:nil];
// TODO: handle the error.
NSLog(@"Error: %@", [error description]);
}
// User canceled the operation.
- (void)wasCancelled:(GMSAutocompleteViewController *)viewController {
[self dismissViewControllerAnimated:YES completion:nil];
}
你不能100%限制它,但你可以通过指定它的东北角和西南角来赋予你的城市更多的优先级, 这是代码
GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
acController.delegate = self;
CLLocationCoordinate2D northEast = CLLocationCoordinate2DMake(northEastLati, northEastLongi);
CLLocationCoordinate2D southWest = CLLocationCoordinate2DMake(southWestLati, southWestLongi);
acController.autocompleteBounds = [[GMSCoordinateBounds alloc] initWithCoordinate:northEast
coordinate:southWest];
[self presentViewController:acController animated:YES completion:nil];
现在您将获得99%的结果将属于您的城市。希望对您有所帮助。
添加 swift 版本。
如果显示自动完成控制器,请通过调用刷新它updateCoordinateBoundsOnAutoCompleteController()
/* Returns Bounds */
func getCoordinateBounds(latitude:CLLocationDegrees ,
longitude:CLLocationDegrees,
distance:Double = 0.001)->GMSCoordinateBounds{
let center = CLLocationCoordinate2D(latitude: latitude,
longitude: longitude)
let northEast = CLLocationCoordinate2D(latitude: center.latitude + distance, longitude: center.longitude + distance)
let southWest = CLLocationCoordinate2D(latitude: center.latitude - distance, longitude: center.longitude - distance)
return GMSCoordinateBounds(coordinate: northEast,
coordinate: southWest)
}
/* Refreshes AutoCompleteController */
func updateCoordinateBoundsOnAutoCompleteController(){
autocompleteController.viewWillAppear(true)
autocompleteController.viewDidAppear(true)
}
下面是 Swift 5 的更新代码。
let autoCompleteVC = GMSAutocompleteViewController()
autoCompleteVC.delegate = self
let addressFilter = GMSAutocompleteFilter()
addressFilter.type = .noFilter
if let currentLocation = locationManager.location?.coordinate {
let northEast = CLLocationCoordinate2D(latitude: currentLocation.latitude + 0.1, longitude: currentLocation.longitude + 0.1)
let southWest = CLLocationCoordinate2D(latitude: currentLocation.latitude - 0.1, longitude: currentLocation.longitude - 0.1)
addressFilter.locationBias = GMSPlaceRectangularLocationOption(northEast, southWest);
}
autoCompleteVC.autocompleteFilter = addressFilter
present(autoCompleteVC, animated: true, completion: nil)