我一直在研究在我的 Flutter 应用程序中运行本机代码以在我的 Google 地图上显示 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 的 pre-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
我环顾四周,但找不到任何可以帮助这个特定案例的东西,因为我认为很多人都没有这样做。我知道 KML 支持已请求 google_maps_flutter 包,但它似乎已经安静了。
对此的任何帮助将不胜感激,因为它是我正在开发的应用程序的核心。没有这个,该应用程序几乎毫无用处。
谢谢