限制Google AutoComplete API只显示附近的位置

对于位置查找Google自动完成的作品不错,但它将search结果返回到世界各地。 我想限制在1000米半径或城市的search结果。 我跟着这个谷歌链接来集成自动完成search。 只有在项目中使用下面的代码

-(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%限制它,但你可以通过指定它的东北angular和西南angular给你的城市更多的优先权,这里是代码

 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版本。

如果您的autocompleteController正在呈现,请通过调用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) }