Home > OS >  How can I get the parameters of my map, to do the search, without having to put everything in the sa
How can I get the parameters of my map, to do the search, without having to put everything in the sa

Time:12-01

I have a problem, I don't know how I can obtain the necessary data to be able to do the address search.

This is the TextFiel that I will use to search for the locations.

class Search extends StatelessWidget {
  const Search({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return search(context);
  }

  search(BuildContext context) {
    return Center(
      child: Column(
        children: [
          SizedBox(
            height: MediaQuery.of(context).size.height * 0.68,
          ),
          CustomTextField(
            hintText: 'A donde vamos?',
            sizeW: MediaQuery.of(context).size.width * 0.9,
          ),
        ],
      ),
    );
  }
}

This is my map where I want to get the information from

class GoogleMapsLocation extends StatefulWidget {
  const GoogleMapsLocation({super.key});

  @override
  State<GoogleMapsLocation> createState() => _GoogleMapsLocationState();
}

class _GoogleMapsLocationState extends State<GoogleMapsLocation> {
  late GoogleMapController googleController;

  CameraPosition _initialCameraPosition = const CameraPosition(
    target: LatLng(37.42796133580664, -122.085749655962),
    zoom: 14.0,
  );

  TextEditingController _searchController = TextEditingController();

  Set<Marker> markers = {};

  Future<void> getLocations() async {
    //Loading circle
    Loading.loadingCircle(context: context);
    Location? placeLocation = await ServicesLocation.getLocation(
        context: context, text: _searchController.text);

    if (placeLocation != null) {
      _initialCameraPosition = CameraPosition(
          target: LatLng(placeLocation.latitude, placeLocation.longitude),
          zoom: 14);
      googleController.animateCamera(
          CameraUpdate.newCameraPosition(_initialCameraPosition));
      markers.add(Marker(
          markerId: const MarkerId('Ubicación lugar'),
          position: LatLng(placeLocation.latitude, placeLocation.longitude)));
      setState(() {});
    }
    Navigator.of(context).pop();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        GoogleMap(
          zoomControlsEnabled: false,
          markers: markers,
          mapType: MapType.terrain,
          initialCameraPosition: _initialCameraPosition,
          onMapCreated: (GoogleMapController controller) {
            googleController = controller;
          },
        ),
        messageAvatar(),
      ],
    );
  }

I would like to be able to search for directions in the search widget, so I don't have to add everything in the google_ map.dart file

CodePudding user response:

I guess what you are looking for is being able to search a location and then this location becomes coordinates for your map. In your example all you have in your app is a map. You need to add geocoding in your app to transform a text input to a longitude and latitude which you will be able to use within your map. Hope this answer your question! Here is a popular lib that allows geocoding: https://pub.dev/packages/geocoding

  • Related