Flutter – Mapbox and Polylines

Hi Friends,

Hope you are all well. In this post let’s talk about bringing maps from a Map service provider called Mapbox and drawing Polylines on the maps.

To learn the basics of flutter get my course – Click here.

Other flutter courses – here. (Affiliate Link)

As we all know we don’t yet have a widget for bringing Google Maps into flutter. We have a map_view plugin that enables users to bring in Static Maps. This was all fine till July 16th 2018. On July 16th Google severely restricted the usage quota of Maps API altogether. So, now free users can access the Static Maps API once per day. Imagine if you had written the code and while testing you made a small error., (You would have to wait till the next day to test it again)

So everyone was looking for an alternative to this and many moved towards openstreetmap. Now, as long-time readers probably know I have already written a post on how to bring leaflet maps into your flutter apps over here.

I would highly recommend reading that post once, coz I’ll be reusing most of the code from there.

Before we start, I would like to say that there are two ways of bringing in mapbox maps into your flutter apps.

  1. Use the flutter_map plugin and specify the mapbox api in the tileset layer.
  2. Use the official flutter-mapbox-gl plugin available here.

In this post, we’ll be making use of the first method since the official flutter-mapbox plugin doesn’t support markers or polylines. (But it does have a lot of animated camera movements)

Now Let’s begin.

 

Go to www.mapbox.com and signup for a free account. The free account allows users upto 50,000 mapviews per month.

Once done, simply login into your account and on the dashboard you would see a default key. Copy it. Now click on Studio (Available on top right, next to Account)

Under styles simply click on the start with basic or pick a map from template. For this demo let’s make use of the Streets map.

You will see a small share and use button on your selected map as shown below

shranduse

Click on it and you will see the below dialog.

use

Copy this Integration URL as well.

Now we’ll be using both the key and Integration URL in our request.

First open pubspec.yaml and add the flutter_map plugin as a dependency.

Create a new Flutter app and add the below code to main.dart file.

Now run the app. You will see a screen as shown below. (Ensure that you fill the url and the token)

Screenshot_20180823-212422

Wow.. Nice right .. ?

That’s how you bring Mapbox maps into your flutter apps.

Markers:

It’s the same as before. No changes. Just modify the code as shown below and add a marker layer.

Now run the app. You will see the below screen.

Screenshot_20180823-214708

Alright, let’s move on to Polylines.

Polygons are an interesting aspect of any map, since they represent boundaries. These polygons are drawn using lines called polylines. In simple terms, polyline is nothing but a line drawn on the map.

Now this plugin exposes three layers namely

  1. Tile Layer – The layer in which the map tiles get loaded.
  2. Marker Layer – The layer in which the markers are imposed over the map.
  3. Polyline Layer – The layer which contains the polylines.

Open main.dart file and modify it as shown below.

Now I have set 5 points as the coordinates of the polygon. (Note that the first and last points are the same).

Then I have added a polyline layer in which I have defined the points and the strokeWidth along with a color.

Run the app and you would see the below screen.

Screenshot_20180823-212621

Nice right ? As of now fill is not supported, so we can’t fill the polygon with a faded color or gradient of our choice.

That’s it. As I said before, this is just one way of bringing mapbox maps into flutter. The other way would be to use the official flutter-mapbox-gl plugin. (I’ll maybe make another video and show how to use that as well).

Hope this video helped you guys. If you found this helpful, kindly share it with someone and help them too.

Donate to keep this site alive – here.

(If you want to donate but can’t, it’s fine just subscribe to my youtube channel).

Join our flutter community – here.

Thanks for reading. Peace.. :)

Liked it? Take a second to support admin on Patreon!
  • Eti Uthakima.

    Wow! This is great, i love the explanations, been following your YouTube tutorials and the text explanations are equally good.

  • Lengyel Zoltán

    Nice one, thank you! :)

    Is there any chance to fill that polygon with color? Even more, with some transparency (e.g.: opacity: 80%)