I'm relying on native code in flutter to load a .kml overlay on to my Google Map (modified GoogleMapController.java).
The .kml overlay is stored here: android/app/src/main/res/raw/borders.kml.
I call my overlay in main.dart like so:
Future<void> addKml(GoogleMapController mapController) async {
const MethodChannel channel = MethodChannel('flutter.native/helper');
try {
int kmlResourceId = await channel.invokeMethod('KML');
return mapController.channel.invokeMethod("map#addKML", <String, dynamic>{
'resourceId': kmlResourceId,
});
} on PlatformException catch (e) {
throw 'Unable to plot map: ${e.message}';
}
}
This is my MainActivity.kt:
class MainActivity: FlutterActivity() {
private val CHANNEL = "flutter.native/helper"
private var mapId: Int? = null
override fun onCreate(savedInstanceState: Bundle?, PersistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, PersistentState)
GeneratedPluginRegistrant.registerWith(FlutterEngine(this));
}
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
MethodChannel(flutterEngine.dartExecutor,CHANNEL).setMethodCallHandler{
call,result ->
if (call.method == "KML") {
result.success (getKMLResource());
} else {
result.notImplemented();
}
}
}
private fun getKMLResource(): Int {
return R.raw.borders;
}
}
and finally, my GoogleMapController.java:
case "map#addKML":
int resourceId = call.argument("resourceId");
try {
KmlLayer kml = new KmlLayer(googleMap, resourceId, context);
kml.addLayerToMap();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
How can I specify a path in flutter to load my .kml from? It seems to be loading from here in kotlin:
return R.raw.borders;
Hope someone can help me out here. Thank you in advance.