We want to have a simple Flutter app with a Google Map embedded in it.
The map should appear on the child screen after User taps on the Show Map button.
The map should show a marker at the user’s current location.
Initial State
The current folder structure of the project is as following:
Initial state of code including settings for .env from the previous post:
I put theme into a separate file for convenience.
Note: theme.dart is optional, use your own setup for theming
Add a new Home screen
We will add a new screen to the project to serve as a home screen: HomeScreen.
Create screens folder and add a new file home.dart. Check in-line comments in the code bellow for explanations.
Remember to update main.dart file to use our new Home Screen
If you follow theme setup from the above part, your app look should be as next:
Now, let’s replace a dummy text with a button.
Idea is next: when a user clicks on the button, we will show interactive Google Map widget.
We will leave Home Screen for a moment, our next step is to add Map Screen.
Add a new Map Screen
We will follow similar concept as with Home Screen.
Create a new file map.dart under screens folder.
Check in-line comments in the code bellow for details.
Now we need to open Map Screen after user click’s on Show Map button on the Home Screen.
It’s a small change in the home.dart file.
Restart your app and click on the Show Map button. You should see a new screen opened with a map rendered all over it.
Use Current User’s Location
In this section, we will request user permission to read the current location.
If user rejects to provide permission, we will use fallback lat and lng values as in the previous section.
Otherwise, we will show Google Map’s Marker at User’s current location.
Note: If you use a simulator (Android/iOS), it has predefined default location values set for you. It can be changed under specific simulator settings.
We will leverage location package, which you installed in the first post.
First, for better code organization, we will create a model to store location data.
Create a new folder models in the src folder, and add a new file user_location.dart.
We will add a simple class to store latitude and long values, so we can reuse it in various places.
Open home.dart screen file and let’s update it. Follow in-line comments in the code below.
And the last part,
we need to update map.dart screen file to use provided location parameter with UserLocation data.
Once you are done updating your code, reload application, you should see request to provide Location permissions,
approve it, and you will see a loading circle animation created with CircularProgressIndicator for a couple of seconds.
If everything worked well, in the console you will see a message:
If not, the application will use fallback location data silently.
Click on the Show Map button, and you should see a marker placed at your Device’s location.
Note: implementation of error/fault tolerance flows is not a part of this post
Summary
In this post we used two packages: google_maps_flutter
and location to show Google Maps embedded into screen
and added request for User’s location to show it also on the mentioned map.
We created a couple of screens and a model to support our goals:
screens/home.dart - contains implementation of Home Screen. It’s a simple screen, with a CTA button to Show Map. On this screen, we implemented request for User’s current location with a simple fallback to use default location if permissions request failed.
screens/map.dart - purpose of this screen is to show interactive/embedded Google Map with current location Marker.
models/user_location.dart - a simple class/model to hold location data. We use an object of this class to pass data from Home Screen to Map Screen
Congratulations, we were able to cover all TODO items from the Desired Outcome.
Now, you should treat well yourself, go and have some tasty gummy bears! ◝(ᵔᵕᵔ)◜