Exploring Google Maps in Flutter

Adding Officially Supported Google Maps in a Flutter app

Deven Joshi
Flutter Community
7 min readNov 30, 2018

--

Google recently published the official Google Maps plugin for Flutter, adding official support for Google Maps into Flutter.

This article will look at how to add Google Maps into a Flutter project and the features and customization available. At the time of writing, this plugin is still in developer preview and may have breaking changes in the future. I will try my best to update the article whenever required.

Note: Since Google Maps relies on an external package and is not inbuilt, I haven’t added this article as part of my Deep Dive series into widgets. Nevertheless, this article will follow a similar pattern.

Setting up our app

Before we start, we need to configure our API keys for our app to work with Google Maps. The general instructions are given here in the package and follow them to set your project up.

In a nutshell:

  1. Create a Google Maps API key here.
  2. Add key to the Android manifest.xml:

3. Add key to iOS:

Add the GoogleMaps import to Runner -> AppDelegate.m and add the API Key. Your file should look like this:

4. Add this to Info.plist

And you’re set!

Adding a Google Map to a screen

To add a Google Map, use a GoogleMap widget in your widget tree. Here is a basic example:

This gives us:

A simple implementation of Google Maps in a Flutter app

This is pretty good for just a few lines of code. But most times, we don’t want an app to have a random map. So, let’s see now how to customize, control the map, add markers, etc.

Changing Map Options

The options parameter allows us to set some default settings like enabling/disabling gestures or setting the default camera position, etc.

Changing map type

We can set the map type using the mapType parameter in GoogleMapOptions.

This can be set to satellite, hybrid, normal or terrain.

Switching to satellite view

Set default camera position

Setting the cameraPosition parameter sets the default camera position to a target. We will look at this in detail when we look at CameraPosition in the next section.

Showing user location

The user’s location can be shown on the map by setting the myLocationEnabled parameter to true. For this to work we must also add the required permissions on both platforms.

On Android:

Add

<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />

or

<uses-permission android:name=”android.permission.ACCESS_COARSE_LOCATION” />

to manifest.xml. We also need to explicitly ask the user for permission.

For this, a package like permission_handler can be used to achieve this.

On iOS:

Add a ‘NSLocationWhenInUseUsageDescription’ key to your ‘Info.plist’ file. This will automatically prompt the user for permissions when the map tries to turn on the My Location layer.

The user’s location is now displayed

If all goes well, this should be the result.

Enabling/Disabling Gestures

GoogleMapOptions gives us various options to either enable or disable specific types of gestures like tilt, zoom, etc.

This code snippet disables the three gestures mentioned.

Moving the camera around

If you noticed, the onMapCreated method in the GoogleMap gave us a GoogleMapController.

We can use this controller to do things like set markers or move the camera around.

Let’s see an example of moving the camera to a specific location, say, the Google HQ.

A variable is used to store an instance of GoogleMapController, allowing us to animate the camera.

Next, we have a FloatingActionButton and with its onPressed set to animate the camera to the latitude and longitude of the Google HQ.

Animating the camera to a specific latitude and longitude

Let’s look at all the other things we can do with moving the camera.

CameraUpdate.newCameraPosition()

This method lets us set the latitude and longitude, zoom, bearing (the orientation of the map in degrees) and tilt (a higher tilt gives a side-view/tilted-view of buildings). Bearing changes the direction the camera points in and not tilt.

This gives us:

Using .newCameraPosition() to update camera view

CameraUpdate.newLatLngBounds()

This sets the camera between two LatLngs.

For example, it makes more sense for an app like Uber to show enough of the map to cover you and the car tasked with picking you up rather than showing a map of the entire city.

The function takes the southwest point and the northeast point to cover on the map.

The second parameter is a padding, let’s see what it does.

Here the southwest point is the city of Paris and northeast is Brussels. Let’s set the padding to 0.0 and try it out.

Padding 0.0

The cities are barely visible. To avoid this, we give the map a higher padding to bring the cities in the map.

Padding 48.0

CameraUpdate.newLatLng()

This function simply sets the camera to a new latitude and longitude while retaining the same zoom factor.

Map scrolls to new LatLng but keeps zoom constant

CameraUpdate.newLatLngZoom()

This is similar to the last function but also allows us to change the zoom factor of the camera.

CameraUpdate.scrollBy()

Simply scrolls the map in the X and/or Y direction by a certain amount.

The first parameter gives shift in X direction whereas the second gives shift in the Y direction.

CameraUpdate.zoomIn(), CameraUpdate.zoomOut()

Simply zooms in or out.

CameraUpdate.zoomBy()

Zooms by the amount given.

CameraUpdate.zoomTo()

Zooms to the specific factor.

Adding markers on the Map

We can use the same GoogleMapController we used earlier to add markers on the map as well.

Dropping a marker at the Googleplex

This is as easy as:

Adding text to markers

We can add information to markers using the infoWindowText parameter.

Adding information to a marker

Changing marker icon

We can change the image being used for the marker using the icon parameter. Let’s use the Flutter icon as a marker.

In the icon parameter we can supply an asset like this:

Using a Flutter icon as a marker

Changing the look of the default marker

We can change color using the icon parameter as well:

We can set the alpha (how opaque the icon is) as well:

And finally, we can rotate the icon as well:

That’s it for this article! I hope you enjoyed it, and leave a few claps if you did. Follow me for more Flutter articles and comment for any feedback you might have about this article.

While writing this article, I came across a repository of quite a few examples by Bhavik Makwana including maps. Check it out here.

Feel free to check out my other profiles and articles as well:

Some of my other articles

--

--

Deven Joshi
Flutter Community

Google Developer Expert, Flutter | Technical Writer | Speaker