2

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

Manas Bam
  • 59
  • 1
  • 6
  • @Darshan Hi I need your help here. You solved a similar doubt could you help me too! – Manas Bam Mar 23 '20 at 09:01
  • "he" taps? I'm not sure there is a plugin to assess the gender of the user and act accordingly. What would you like to happen if "she" taps it? :) – Matt Booth Apr 13 '23 at 06:08
  • @MattBooth well pointed out. My 9th Standard self seems to have missed an important segment! – Manas Bam Apr 14 '23 at 10:00

1 Answers1

0

I think I got the answer. I had forgotten to create a billing account in my google cloud platform.

Manas Bam
  • 59
  • 1
  • 6