我想在 flutter 中加载谷歌地图上的 kml 文件。我在 google _maps_flutter 插件上找不到它。有没有其他插件可以在 flutter 中做到这一点?
最佳答案
已经一个月了,所以您可能已经明白了这一点,但希望这对您有所帮助,如果您还没有的话。
你可以在 flutter 中运行原生代码,所以如果你能适应它,它应该可以工作。您将需要创建一个方法 channel 来使用类似这样的东西运行 native 代码。
// 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
即使我已经设法让 native 代码工作,只是显示一些文本,我还没有想出如何让 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
我希望能让你走上正确的道路。
关于google-maps - google_maps_flutter 或任何其他用于 flutter 的 map 插件是否支持谷歌地图的 kml 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58242229/
相关文章:
aws-lambda - 如何使用 CloudFormation 添加订阅过滤器?
asp.net-core-webapi - .NET Core 3 API - 在一个请求后停止工作
r - 在 R 中使用 RCurl 下载 NASA 卫星数据
reactjs - React Native - SetTimeout() 不工作。 t.apply
angular - Ionic Barcode Scanner 插件关闭按钮
swift - animateTransition 不再适用于 iOS 13 中以模态呈现的 vie
objective-c - 无法编译使用 Objective-C 包的 Swift 包