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

Ios 限制Google AutoComplete API仅显示附近的位置,ios,objective-c,google-maps,autocomplete,ios9,Ios,Objective C,Google Maps,Autocomplete,Ios9,对于位置查找,Google AutoComplete效果不错,但它会在全世界返回搜索结果。我想将搜索结果限制在1000米半径内或城市内。我遵循这一点来集成自动完成搜索。只有以下代码在项目中使用 -(void)autoComplete { GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init]; acController.delegate = self;

对于位置查找,Google AutoComplete效果不错,但它会在全世界返回搜索结果。我想将搜索结果限制在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];
}

你不能百分之百地限制它,但是你可以通过指定城市的东北角和西南角来给你的城市更多的优先权,下面是代码

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,请通过调用 updateCoordinationBoundsonAutoCompleteController()

/* 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)
    }

你为什么不使用苹果地图来做同样的事情呢?你用这个自动完成的目的是什么。。。可能显示在表格视图中,或者全景视图这只是地图的一部分。。如果您知道ola搜索栏,您可以通过消费者输入查看附近的位置。我需要实现这一点。它非常适合通过用户输入显示位置列表,但如果用户“A”显示的是美国、阿尔及利亚、亚利桑那州等,而不是这个,我想过滤它并显示附近的位置@BharathVankireddy&Anbu.KarthikI是google开发框架的大恨恶者,因为我觉得苹果框架像vanila口味的冰淇淋,但我需要实现谷歌自动完成功能这是我的前辈们的决定我不想说服他们改变它,他们有足够的理由告诉你为什么谷歌这个任务@bharathvankiredyplace autocomplete不是合适的API,如果你想要附近的位置。即便如此,对于iOS客户端,也无法进行设置。如果您查看GooglePlaceAPI文档,就可以使用PlaceSearch(web服务调用)来实现您想要的功能。