Search code examples
fluttersearchcoordinates

Obtain coordinates from an address flutter


Could someone guide me to get the coordinates of an address in flutter?

I need to enter addresses in a text box and get the longitude and latitude of that address


Solution

  • Here created an Input Text Widget and when user taps on it. This takes to google autocomplete screen, where user inputs the location.

    TextFormField(
                      decoration: new InputDecoration(
                          border: InputBorder.none,
                          contentPadding: EdgeInsets.only(left: 15),
                          hintText: Strings.enter_your_house_number_street_etc,
                          hintStyle: TextStyle(
                              fontSize: 14,
                              color: AppColor.grey,
                              fontFamily: "Open Sans",
                              fontWeight: FontWeight.normal
                          )),
                      maxLines: 1,
                      controller: _address,
                    onTap: ()async{
                      // then get the Prediction selected
                      Prediction p = await PlacesAutocomplete.show(
                          context: context, apiKey: kGoogleApiKey,
                      onError: onError);
                      displayPrediction(p);
                    },
                    )
    

    Here it is getting the lat and long of entered location.

    Future<Null> displayPrediction(Prediction p) async {
        if (p != null) {
          PlacesDetailsResponse detail = await _places.getDetailsByPlaceId(p.placeId);
    
          var placeId = p.placeId;
           lat = detail.result.geometry.location.lat;
           long = detail.result.geometry.location.lng;
    
          var address  =detail.result.formattedAddress;
    
          print(lat);
          print(long);
          print(address);
    
          setState(() {
            _address.text = address;
          });
        }
      }
    

    import 'package:flutter_google_places/flutter_google_places.dart';