Hello and welcome back!
In this tutorial, we are going to see how to embed or display google maps in a flutter map. Google Maps are something you will eventually be using in your apps at some point in the future. It has wide use cases and amazing APIs to bring more features to your app.
In this tutorial, we are going to use google_maps_flutter package which is developed and maintained by the flutter team for easy integration of maps in flutter apps. Without further ado, let's see the video illustration of what we are going to achieve in this tutorial.
NOTE: To access Google Maps API you need to:
- Have a payment verified google cloud platform account
- Enable Maps SDK by searching 'maps' in the marketplace
- Have an API Key generated from the google cloud console
STEPS TO REPRODUCE:
- Add package dependencies in the pubspec.yaml file
dependencies: flutter: sdk: flutter google_maps_flutter: any
- Add the following code in your AndroidManifest.xml file ("android/app/src/main/AndroidManifest.xml") JUST AFTER </activity>
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="YOUR API KEY"/>
- Finally, use the following code in your main.dart file
import 'package:flutter/material.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( debugShowCheckedModeBanner: false, title: 'Google Maps', theme: ThemeData( primaryColor: Colors.blue, ), home: MyHomePage(title: 'Google Maps'), ); } } class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { GoogleMapController mapController; @override void initState() { super.initState(); } void _onMapCreated(GoogleMapController controller) { mapController = controller; } @override Widget build(BuildContext context) { Size size = MediaQuery.of(context).size; return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Column( children: <Widget>[ Container( height: size.height * 0.85, child: GoogleMap( myLocationEnabled: true, myLocationButtonEnabled: true, initialCameraPosition: CameraPosition( target: LatLng(37.42796133580664, -122.085749655962), zoom: 15.0, ), key: ValueKey('uniqueey'), onMapCreated: _onMapCreated, markers: { Marker( markerId: MarkerId('anyUniqueId'), position: LatLng(37.42796133580664, -122.085749655962), infoWindow: InfoWindow(title: 'Some Location')) }, ), ), ], ), ); } }
That's all! You should be able to get the google maps running in your app.
Please Note:
You have added the code in AndroidManifest.xml at the right location
If you still face issues, run command flutter clean and then flutter run again!