我想在Google Map上快速加载kml文件。我在Google _maps_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);
}
}
可以在下面的URL中找到KML层代码。
https://github.com/googlemaps/android-maps-utils/commit/d606fcde40467abb5fae2ba78b8562a2cd1c517b
尽管我已经设法通过显示一些文本来使本机代码正常工作,但我还没有弄清楚如何使KML代码正常工作。我认为问题在于它不知道mMap中的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();
}
}
}
}
您可以在这里看到更多详细信息。
希望您能走对路。