1

I am using the official Google Documentation to create the Address lookup.

In the Android version I am able to bias the result to my area.

For example typing in "123 Main St" shows the Main st in my town first before showing me other results.

In iOS I can't figure out how to do that. I am using the "Add a Full-Screen Control" with the the GMSAutocompleteViewController delegate.

There is a GMSCoordinateBounds but the example is for a map. I am not using a map.

I have tinkered around with something like this but can't get it to work. Am I even on the right track?

- (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);
                          }
                        }];
}

Any help?

JasonBourne
  • 338
  • 2
  • 13

2 Answers2

2

You can set a bounds on the GMSAutocompleteViewController directly, for example:

// 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)
}
AndrewR
  • 10,759
  • 10
  • 45
  • 56
  • Thanks, but how are you calculating the lat/lon of your bounding box? Are you staring in the center of where you are and expanding out 20 miles? – JasonBourne Feb 09 '17 at 14:15
  • I don't know about the code above (copypasta from Google's dev site) but if you want to calculate a 20 mile bounding box around a latlng point, the methods in SphericalUtil will help (https://developers.google.com/maps/documentation/android-api/utility/#spherical) – AndrewR Feb 10 '17 at 03:42
0

Create a bounds based on your location and set it as the bounds of GMSAutocompleteViewController

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

here currentLocation is the current device location in CLLocationCoordinate2D!

Arun Basil Issac
  • 275
  • 2
  • 10