对于位置查找,Google自动完成工作正常,但它会在全球范围内返回搜索结果。我想限制在1000米半径或城市中的搜索结果。我按照此google link集成了自动完成搜索。仅在项目
中使用以下代码-(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];
}
答案 0 :(得分:2)
你不能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%的结果属于你的城市。希望它会对你有所帮助。
答案 1 :(得分:0)
添加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)
}