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:
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.
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.
I figured it out. The _search()
function has a radius radius: center == null ? null : 10000);
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.
检查您的帐单是否已在 Google APIS 上激活
Look in the console for errors. In my case, my billing was linked and relevent APIs was enabled, still it never show predictions. Then I added the following strictBounds & types as non-null values.
Prediction p = await PlacesAutocomplete.show(
context: context,
apiKey: Local.googleMapsAPIKey,
radius: 10000000,
types: [],
strictbounds: false,
mode: Mode.overlay,
language: "fr",
decoration: InputDecoration(
hintText: 'Search',
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
borderSide: BorderSide(
color: Colors.white,
),
),
),
components: [Component(Component.country, "fr")],
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.