google_maps_flutter或flutter的任何其他地图插件是否支持Google地图的kml文件?

时间:2019-10-04 19:31:42

标签: google-maps flutter kml google-maps-flutter flutter-widget

我想在Google Map上快速加载kml文件。我在Google _maps_flutter插件上找不到它。还有其他插件可以做到这一点吗?

1 个答案:

答案 0 :(得分:0)

已经一个月了,所以您可能已经知道了,但是希望,如果没有,这可以帮助您。

您可以在Flutter中运行本机代码,因此,如果您可以进行调整,它应该可以工作。您将需要创建一个“方法通道”来运行带有此类内容的本机代码。

// 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层代码可在下面的URL中找到。

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

即使我设法通过显示一些文本就可以使本机代码正常工作,但我还没有弄清楚如何使KML代码正常工作。我认为问题在于它不知道mMap的含义。 onPostExecute方法,但很有可能还有很多我不知道的方法。

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

您可以在此处看到更多详细信息。

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

希望您能走对路。