Flutter how to get google map nearby places automatically with user's current location?

Viewed 17
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'dart:async';


void main() {
  runApp(MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MapSample(),

    );
  }
}

class MapSample extends StatefulWidget {
  @override
  State<MapSample> createState() => MapSampleState();
}

class MapSampleState extends State<MapSample> {
  Future<Position> _determinePosition() async {
    Position position = await Geolocator.getCurrentPosition();
    return position;
  }
  late GoogleMapController googleMapController;


  static final CameraPosition _kGooglePlex = CameraPosition(
    target: LatLng(37.42796133580664, -122.085749655962),
    zoom: 14.4746,
  );


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Medical Location"),
        centerTitle: true,
      ),
      body: GoogleMap(
        mapType: MapType.hybrid,
        // markers: {_kGooglePlexMarker},
        initialCameraPosition: _kGooglePlex,
        onMapCreated: (GoogleMapController controller) {
          googleMapController = controller;
        },
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () async {
          Position position = await _determinePosition();

          googleMapController.animateCamera(CameraUpdate.newCameraPosition(
              CameraPosition(
                  target: LatLng(position.latitude, position.longitude),
                  zoom: 14)));
          setState(() {});

        },
        label: Text('Current Location'),
        icon: Icon(Icons.location_history),
      ),
    );
  }
}

Hello, this is the code I've been working on and it shows users' location on the map when they click the 'current location' button. What I want to implement is to make the map shows nearby hospitals based on the user's current location. I've searched for documents about this problem but couldn't find one that really can help me. I'm getting the current location by 'LatLng(position.latitude, position.longitude)' which is not specific numbers fixed because location can vary. I think I need to use 'places API' but except for that, I couldn't figure solutions out yet. Could anyone help me to solve this problem by explaining codes I need to add? Thank you so much

0 Answers
Related