如何在Flutter中包含本机代码以在Google Map上显示KML图层?

时间:2019-11-19 11:54:16

标签: google-maps flutter kml native-code google-maps-flutter

我一直在研究在Flutter应用中运行本机代码以在我的Google Map上显示KML图层,尽管我确实在运行本机代码(至少在Android上),但我在获取KML方面遇到了困难工作的东西。

我在Flutter类中创建了一个MethodChannel来运行本机代码,这可以正常工作。请参见下面。

// 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);
    }
  }

我正在获取一些我仅在Android的预Alpha版本的应用程序上使用的代码,所以我知道它可以正常工作,但是有4个错误。

在我的MainActivity java文件中,我有这些软件包。

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;

和MainActivity类由这个组成。

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();
      }
    }
  }
}

这是我的错误

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

我环顾四周,但找不到任何能帮助解决此特定情况的东西,因为我认为没有很多人对此做任何事情。我知道有人要求对google_maps_flutter软件包提供KML支持,但它似乎已经安静了。

对此的任何帮助将不胜感激,因为它对我正在开发的应用程序非常重要。没有这个,该应用程序将几乎无用。

谢谢

0 个答案:

没有答案