Show Line Data from GeoJSON File on the Map


This example demonstrates how to load and parse bundled GeoJSON resource and display it on the map.

You will learn the following:

  • How to load GeoJSON from file (bundled as a local asset on the device) on a background thread.
  • How to draw lines on the map.

Include Raster to the project

In Android Studio, copy your GeoJSON file into assets folder (create assets folder if it does not exists). The path should be app/src/main/assets.

Initialize Map

We will add GeoJSON overlay to the map after the style has been loaded. So firstly we initialize the mapView in onViewCreated method.

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val mapTilerKey = Helper.getMapTilerKey(context!!)
        val styleUrl = "${mapTilerKey}";

        // Create map view
        mapView = view.findViewById(
        mapView?.getMapAsync { map ->

            // Navigate the map to the area with geojson data
            map.cameraPosition = CameraPosition.Builder()
                .target(LatLng(45.5076, -122.6736))

            // Load the style and load geoJson after the style has been loaded
            map.setStyle(styleUrl) {
                GeoJsonLoader(this, it).execute()

In this method we initiate GeoJSON parsing on background thread

Load GeoJSON and create Feature Collection

The code for loading stream with GeoJSON data and parsing it into FeatureCollection is in GeoJsonLoader class. This class inherits from AsyncTask and it loads and parses GeoJSON in doInBackground method.

override fun doInBackground(vararg params: Void): FeatureCollection? {
        val fragment: GeoJsonOverlayFragment? = weakReference.get()
        if (fragment != null) {
            val context = fragment.activity?.applicationContext
            if (context != null) {
                val inputStream: InputStream ="example.geojson")
                return FeatureCollection.fromJson(convertStreamToString(inputStream))

        return null

When the FeatureCollection is created, the class initiates drawing from onPostExecute method.

override fun onPostExecute(featureCollection: FeatureCollection?) {
        val fragment: GeoJsonOverlayFragment? = weakReference.get()
        if (fragment != null && featureCollection != null) {

Draw lines on the map

Finally, we retrieve the map style and add the new source using the feature collection created in previous step. Then we add line layer and configure its style.

private fun drawLines(featureCollection: FeatureCollection) {
        if (featureCollection.features() != null) {
            if (featureCollection.features()!!.size > 0) {
                style.addSource(GeoJsonSource("line-source", featureCollection))

                // The layer properties for our line. This is where we make the line dotted, set the
                // color, etc.
                    LineLayer("linelayer", "line-source")