1

I've been looking into running native code in my Flutter app to display a KML Layer on my Google Map and while I do have the app running native code (at least on Android), I am having difficulty with getting the KML stuff to work.

I have created a MethodChannel in my Flutter class to run the native code and this works fine. Please see below.

// Run java code for KML Campus Map Overlay
  Future<void> _showCampusMap() async {
    const platform = MethodChannel('uk.ac.manchestermaps/kmlLayer');
    try {
      final campusMapOverlay =
          await platform.invokeMethod('retrieveFileFromUrl');
      print(campusMapOverlay);
    } on PlatformException catch (error) {
      print(error);
    }
  }

I am taking some code that I used on the Android only pre-alpha version of the app, so I know it works, but I have 4 errors.

In my MainActivity java file,I have these package.

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;

and the MainActivity class consists of this.

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

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

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

      }
    });
  }

  private void retrieveFileFromUrl() {
    new DownloadKmlFile("**<REMOTE KML FILE>**")
        .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();
      }
    }
  }
}

Here are my errors

Running Gradle task 'assembleDebug'...

.....\MainActivity.java:73: error: cannot find symbol
        KmlLayer kmlLayer = new KmlLayer(mMap, new ByteArrayInputStream(byteArr), getApplicationContext());
        ^

  symbol:   class KmlLayer

  location: class MainActivity.DownloadKmlFile
.....\MainActivity.java:73: error: cannot find symbol
        KmlLayer kmlLayer = new KmlLayer(mMap, new ByteArrayInputStream(byteArr), getApplicationContext());
                                ^

  symbol:   class KmlLayer

  location: class MainActivity.DownloadKmlFile
.....\MainActivity.java:73: error: cannot find symbol
        KmlLayer kmlLayer = new KmlLayer(mMap, new ByteArrayInputStream(byteArr), getApplicationContext());
                                         ^

  symbol:   variable mMap
  location: class MainActivity.DownloadKmlFile

3 errors

I have looked around, but can't find anything to help this this specific case as I don't think a lot of people are doing on it. I know KML support has been request for the google_maps_flutter package, but it seems to have gone quiet.

Any help with this would be very much appreciated, as it is pretty central to the app I am developing. Without this, the app is next to useless.

Thanks

Andrew Stevenson
  • 578
  • 1
  • 9
  • 23
  • did u get success for loading kml layer in flutter? – Code Runner Mar 11 '20 at 18:46
  • Not yet - I've had to work on other things within my app, but recently got back to it. I've a small XML reader that retrieves the building name of the KML file, but I need to go deeper to get this. It's only from a local file currently, but I need it to be remote. I'm looking at using the google_map_polyline package to show the polylines & have got this working to show directions. https://github.com/Shark01/google_map_polyline/blob/master/example/lib/main.dart I'm not there yet, but can see a possible solution. I don't think we're going to hear from that guy who has written a KML reader. – Andrew Stevenson Mar 12 '20 at 14:18
  • you are right. I am waiting for that guy's reply too. I am learning to develop plugin in flutter can you tell me how you are integrating other plugins into your own plugin project to develop some functionality or extended functionality based on specific plugin. like you might installed google_maps_flutter to develop kml layer so just wanted to know how you are working with google maps in your plugin. – Code Runner Mar 12 '20 at 16:36
  • I am simply adding them to my pubspec.yaml, then importing them in the standard way. Nothing out of the ordinary – Andrew Stevenson Mar 12 '20 at 17:02

0 Answers0