Flutter/Google Maps/
2022-10-17T09:34:04.580794Z
Published on
Google Maps Flutter plugin is provided in the Google Map widget that supports initialCameraPosition, maptype, and onMapCreated. We can set the position of the camera and marker in any place on the earth. We can design the marker according to our choice. It also comes with a zoom property in a cameraposition to provide the zooming-in google map view on the initial page
Google cloud account ( https://console.cloud.google.com )
Plugin used : google_maps_flutter: ^2.1.12
( google_maps_flutter | Flutter Package )
If you don't have an Google Cloud account (https://console.cloud.google.com ), please proceed with the signup option. otherwise, log in with your existing credentials.
In the Google Cloud dashboard click Select a project
It prompts a popup, In that popup click NEW PROJECT
On the next page fill project name and click CREATE
In the Google Cloud dashboard Select a project (i.e. Created in Step 3 : Create a project in Google Cloud )
After selecting your project go to APIs and services > Library
On the next page, click Maps SDK for Android (Based on your device Android or iOS)
On the next page click ENABLE
In the Google Cloud dashboard Select a project (i.e. Created in Step 3 : Create a project in Google Cloud )
After selecting your project go to APIs and services > Credentials
Click on CREATE CREDENTIALS > API key.
You will get a popup with an API key
Initially, the restrictions will be set to None. If you want to restrict your key,
In the dashboard Navigate to APIs and services > Credentials > click Edit API key
On the next page fill the following details
In Application restrictions > choose Android apps (based on your device Android or iOS)
In Restrict usage to your Android apps click ADD AN ITEM.
Add your package name(i.e. In your flutter Project’s AndroidManifest.xml file package="com.example.map_project"
)
Add your SHA-1 certificate fingerprint.
To generate a SHA-1 certificate fingerprint. In Android studio terminal navigate to your project directory and run the following command
for windows
1keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
for Linux or macOS
1keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
Copy the SHA-1 certificate fingerprint. Add your SHA-1 certificate fingerprint and click DONE
In API restrictions choose to Restrict key
In the select APIs dropdown select Map SDK for Android. click OK > SAVE
If Map SDK is not in the list, you have to enable it first.
Open Android Studio and go to File > New > New Flutter Project
It prompts a popup. In that popup, browse your Flutter SDK installation path and click Next
On the next popup fill project name, project location and click Finish
Create a dart file named current_loaction.dart under the lib folder.
The final project structure is given below
Add the google_maps_flutter: ^2.1.12
package in your pubspec.yml and run pub get
For Android
Set the minSdkVersion
in android/app/build.gradle
:
1android {
2 defaultConfig {
3 minSdkVersion 20
4 }
5}
.
Specify your API key(i.e. created in Step 5: To retrieve the API key) in the application manifest android/app/src/main/AndroidManifest.xml
:
1<manifest ...
2 <application ...
3 <meta-data android:name="com.google.android.geo.API_KEY"
4 android:value="YOUR KEY HERE"/>
also, add permission to use location in AndroidManifest.xml
:
1<manifest ...
2<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
3 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
4 <uses-permission android:name="android.permission.INTERNET"/>
.
As part of the google maps integration on this project, we are using geolocator: ^9.0.1
plugin to get the current location.
In main.dart – get the latitude
and longitude
values using geolocator.
A sample main.dart file can be found in the following git repository file.
In current_location.dart – Using the latitude
and longitude
values we will get the location in google Maps.
1GoogleMap(
2 onMapCreated: _onMapCreated,
3 initialCameraPosition: CameraPosition(
4 target: LatLng(double.parse(Lat) , double.parse(Lng)),
5 zoom: 20.0,
6 ),
7 )
onMapCreated
– This method is called on map creation and takes a MapController as a parameter.
initialCameraPosition
– This gives the starting position of the map when it is opened for the first time.
A sample current_location.dart file can be found in the following git repository file.
Complete Flutter example in the following github repo.
After run the app , If we click on the Get current location button, we will get the current location. See the below image:
Comments