google-mapsflutterdartgoogle-places-apigoogle-places-autocomplete

flutter_google_places not showing autocomplete search results


I am following along with the tutorial from the following link on using Google Maps API to create a search function to look for places on a map:

Medium.com flutter tutorial

Below is the code that I believe constitutes the core of the issue:

 /**
* Retrieves the user's location
* NOTE: This pretty much does the same thing as _animateToUser,
* but I separated the two
*/
Future<LatLng> getUserLocation() async {
 var currentLocation = <String, double>{};
 final uLocation = LocationManager.Location();
try {
  currentLocation = await uLocation.getLocation();
  final lat = currentLocation["latitude"];
  final lng = currentLocation["longitude"];
  final center = LatLng(lat, lng);
  return center;
} on Exception {
  currentLocation = null;
  return null;
}
}

/**
 * Searches for locations using Google Maps API
*/
Future<void> _search() async {
try {
  final center = await getUserLocation();
  Prediction p = await PlacesAutocomplete.show(
      context: context,
      strictbounds: center == null ? false : true,
      apiKey: kGoogleApiKey,
      onError: onError,
      mode: Mode.overlay,
      language: "en",
      location: center == null
          ? null
          : Location(center.latitude, center.longitude),
      radius: center == null ? null : 10000);

  showDetailPlace(p.placeId);
} catch (e) {
  return;
}
}

/**
* Shows details of a place
*/
Future<Null> showDetailPlace(String placeId) async {
if (placeId != null) {
  Navigator.push(
    context,
    MaterialPageRoute(builder: (context) => 
    PlaceDetailWidget(placeId)),
  );
 }
 } /**
 * Retrieves the user's location
 * NOTE: This pretty much does the same thing as _animateToUser,
 * but I separated the two
 */
Future<LatLng> getUserLocation() async {
var currentLocation = <String, double>{};
final uLocation = LocationManager.Location();
try {
  currentLocation = await uLocation.getLocation();
  final lat = currentLocation["latitude"];
  final lng = currentLocation["longitude"];
  final center = LatLng(lat, lng);
  return center;
} on Exception {
  currentLocation = null;
  return null;
}
}`

The problem is that, no matter what I type into the search bar, it will not autocomplete/suggest anything.

Example: enter image description here

In this example I want to bring up a list of places that have the word grand in them like 'Grand Central Station', for instance.


Solution

  • I figured it out. The _search() function has a radius radius: center == null ? null : 10000); that it uses to calculate areas around you within the radius you give it. My emulator has my location as being in the middle of the Atlantic Ocean (separate issue, but very minor). Since there were no places that start with 'grand' near my location in the middle of the ocean, nothing was coming up. I increased the radius from 10000 to 10000000 and it started to find locations.