'Does google_maps_flutter or any other map plugin for flutter supports kml file for google maps?

I want to load kml files on google map in flutter. i can't find it on google _maps_flutter plugin. is there any other plugins which can do it in flutter?



Solution 1:[1]

It's been a month, so you may have figured this out, but hopefully, if you haven't this can help.

You can run native code in flutter, so if you can adapt this, it should work. You will need to create a Method Channel to run the native code with something like this.

// Run java code for KML Campus Map Overlay
  Future<void> _showCampusMap() async {
    const platform = MethodChannel(**<YOUR METHOD CHANNEL>**);
    try {
      final campusMapOverlay = await platform.invokeMethod('downloadKmlLayer');
      print(campusMapOverlay);
    } on PlatformException catch (error) {
      print(error);
    }
  }

KML Layer code can be found in the URL below.

https://github.com/googlemaps/android-maps-utils/commit/d606fcde40467abb5fae2ba78b8562a2cd1c517b

Even though I have managed to get native code to work, with simply displaying some text, I haven't figured out how to get the KML Code to work yet.I think the problem lies in it not knowing what mMap is in the onPostExecute method, but it is very possible there is more than I do not know.

    import java.io.Console;    
    import android.os.Bundle;
    import io.flutter.app.FlutterActivity;
    import io.flutter.plugins.GeneratedPluginRegistrant;
    import io.flutter.plugin.common.MethodCall;
    import io.flutter.plugin.common.MethodChannel;
    import io.flutter.plugin.common.MethodChannel.Result;
    import io.flutter.plugin.common.MethodChannel.MethodCallHandler;

    import org.xmlpull.v1.XmlPullParserException;
    import android.os.AsyncTask;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URL;


    public class MainActivity extends FlutterActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);

        new MethodChannel(getFlutterView(), "**<YOUR METHOD CHANNEL>**").setMethodCallHandler(new MethodCallHandler() {
          @Override
          public void onMethodCall(MethodCall call, MethodChannel.Result result) {

            if (call.method.equals("retrieveFileFromUrl")) {
              String KMLLayer = retrieveFileFromUrl();
              result.success(KMLLayer);
            }

          }
        });
      }

      private void retrieveFileFromUrl() {
        new DownloadKmlFile("**<YOUR KML LAYER>**")
                .execute();
      }

      private class DownloadKmlFile extends AsyncTask<String, Void, byte[]> {
        private final String mUrl;

        public DownloadKmlFile(String url) {
          mUrl = url;
        }

        protected byte[] doInBackground(String... params) {

          try {
            InputStream is = new URL(mUrl).openStream();
            // Log.d(TAG, "doInBackground: " + mUrl.toString());
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int nRead;
            byte[] data = new byte[16384];
            while ((nRead = is.read(data, 0, data.length)) != -1) {
              buffer.write(data, 0, nRead);
            }
            buffer.flush();
            return buffer.toByteArray();
          } catch (IOException e) {
            e.printStackTrace();
          }
          return null;
        }

        protected void onPostExecute(byte[] byteArr) {
          try {
            KmlLayer kmlLayer = new KmlLayer(mMap, new ByteArrayInputStream(byteArr), getApplicationContext());
            kmlLayer.addLayerToMap();
            // moveCameraToKml(kmlLayer);
          } catch (XmlPullParserException e) {
            e.printStackTrace();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }

You can see here for a bit more details.

https://medium.com/47billion/creating-a-bridge-in-flutter-between-dart-and-native-code-in-java-or-objectivec-5f80fd0cd713

I hope gets you on the right path.

Solution 2:[2]

--------NOTE: only for android-------------

This solution won't work for iOS but there is a workaround for android, you can see the solution here in the medium article

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Code Runner