In my app I want the user to type out a place(and flutter_google_places must show suggestions of places). Then I want to get the latitude and longitude of that place he taps.
Problem:I have seen this answer:How to create a simple google maps address search with autocomplete in flutter and get latitude and longitude?
which is similar to my question but it does not work for me!
My code:
Completer<GoogleMapController> _controller = Completer();
final api_key=API_KEY;
GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: API_KEY);
LocationData user_location=LoginPage.LD;
@override
Widget build(BuildContext context) {
CameraPosition cuurent_location_of_user = CameraPosition(
target: LatLng(user_location.latitude,user_location.longitude),
zoom: 16,
);
return Scaffold(
body: Stack(
children: <Widget>[
Container(
child:GoogleMap(
mapType: MapType.hybrid,
initialCameraPosition: cuurent_location_of_user,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
)
),
Container(
alignment: Alignment.center,
child: RaisedButton(
onPressed: () async {
Prediction p = await PlacesAutocomplete.show(
context: context, apiKey: api_key);
displayPrediction(p);
},
)
),
],
),
);
}
Future<Null> displayPrediction(Prediction p) async {
if (p != null) {
PlacesDetailsResponse detail =
await _places.getDetailsByPlaceId(p.placeId);
var placeId = p.placeId;
double lat = detail.result.geometry.location.lat;
double lng = detail.result.geometry.location.lng;
var address = await Geocoder.local.findAddressesFromQuery(p.description);
print(lat);
print(lng);
}
}
If I try to: print(p.toString()); It returns null I am unable to find the problem in my code I have also enabled the places API in Google Cloud Service and I have taken the most recent API_KEY as well
Thank You