As the subject says. I've got how to do it in Kotlin:
fun addGeoJsonPolygonToMap(geoPoints: Array<GeoPoint>,id: String, fillColor: String, Opacity: Double, fontSize: Double){
val mutableList : MutableList<Point> = ArrayList()
for(p in geoPoints){
val currentPoint = Point.fromLngLat(p.longitude, p.latitude)
mutableList.add(currentPoint)
}
val item = GeoJsonSource.Builder(id)
.geometry(Polygon.fromLngLats(listOf(mutableList)))
.build()
mapView!!.getMapboxMap().getStyle()?.addSource(item)
val polygonLayer = FillLayer(id + "FillLayer_ID", id)
polygonLayer.fillColor(fillColor)
polygonLayer.fillOpacity(Opacity)
val symbolLayer = SymbolLayer(id + "SymbolLAYER_ID", id)
symbolLayer.textField(id)
symbolLayer.textSize(fontSize)
symbolLayer.textColor(fillColor)
symbolLayer.textOpacity(1.0)
try {
mapView!!.getMapboxMap().getStyle()?.addPersistentLayer(polygonLayer)
mapView!!.getMapboxMap().getStyle()?.addPersistentLayer(symbolLayer)
} catch(ex: Exception){
Log.d("removeAnnotation",ex.message!!)
}
}
I've got the generated kotlin byte code that I'm trying to translate:
public final void addGeoJsonPolygonToMap(@NotNull GeoPoint[] geoPoints, @NotNull String id, @NotNull String fillColor, double Opacity, double fontSize) {
Intrinsics.checkNotNullParameter(geoPoints, "geoPoints");
Intrinsics.checkNotNullParameter(id, "id");
Intrinsics.checkNotNullParameter(fillColor, "fillColor");
List mutableList = (List)(new ArrayList());
GeoPoint[] var11 = geoPoints;
int var12 = geoPoints.length;
for(int var10 = 0; var10 < var12; ++var10) {
GeoPoint p = var11[var10];
Point currentPoint = Point.fromLngLat(p.getLongitude(), p.getLatitude());
Intrinsics.checkNotNullExpressionValue(currentPoint, "currentPoint");
mutableList.add(currentPoint);
}
GeoJsonSource.Builder var10000 = new GeoJsonSource.Builder(id);
Polygon var10001 = Polygon.fromLngLats(CollectionsKt.listOf(mutableList));
Intrinsics.checkNotNullExpressionValue(var10001, "Polygon.fromLngLats(listOf(mutableList))");
GeoJsonSource item = Builder.geometry$default(var10000, (Geometry)var10001, (String)null, 2, (Object)null).build();
MapView var18 = this.mapView;
Intrinsics.checkNotNull(var18);
Style var19 = var18.getMapboxMap().getStyle();
if (var19 != null) {
SourceUtils.addSource((StyleInterface)var19, (StyleContract.StyleSourceExtension)item);
}
FillLayer polygonLayer = new FillLayer(id + "FillLayer_ID", id);
polygonLayer.fillColor(fillColor);
polygonLayer.fillOpacity(Opacity);
SymbolLayer symbolLayer = new SymbolLayer(id + "SymbolLAYER_ID", id);
symbolLayer.textField(id);
symbolLayer.textSize(fontSize);
symbolLayer.textColor(fillColor);
symbolLayer.textOpacity(1.0);
try {
var18 = this.mapView;
Intrinsics.checkNotNull(var18);
var19 = var18.getMapboxMap().getStyle();
if (var19 != null) {
LayerUtils.addPersistentLayer$default((StyleInterface)var19, (Layer)polygonLayer, (LayerPosition)null, 2, (Object)null);
}
var18 = this.mapView;
Intrinsics.checkNotNull(var18);
var19 = var18.getMapboxMap().getStyle();
if (var19 != null) {
LayerUtils.addPersistentLayer$default((StyleInterface)var19, (Layer)symbolLayer, (LayerPosition)null, 2, (Object)null);
}
} catch (Exception var14) {
String var20 = var14.getMessage();
Intrinsics.checkNotNull(var20);
Log.d("removeAnnotation", var20);
}
}
And then my translation:
public final void addGeoJsonPolygonToMap(@NotNull GeoPoint[] geoPoints, @NotNull String id, @NotNull String fillColor, double Opacity, double fontSize) {
Intrinsics.checkNotNullParameter(geoPoints, "geoPoints");
Intrinsics.checkNotNullParameter(id, "id");
Intrinsics.checkNotNullParameter(fillColor, "fillColor");
List mutableList = (List)(new ArrayList());
GeoPoint[] var11 = geoPoints;
int var12 = geoPoints.length;
for(int var10 = 0; var10 < var12; ++var10) {
GeoPoint p = var11[var10];
Point currentPoint = Point.fromLngLat(p.getLongitude(), p.getLatitude());
Intrinsics.checkNotNullExpressionValue(currentPoint, "currentPoint");
mutableList.add(currentPoint);
}
GeoJsonSource.Builder var10000 = new GeoJsonSource.Builder(id);
Polygon var10001 = Polygon.fromLngLats(CollectionsKt.listOf(mutableList));
Intrinsics.checkNotNullExpressionValue(var10001, "Polygon.fromLngLats(listOf(mutableList))");
GeoJsonSource item = Builder.geometry$default(var10000, (Geometry)var10001, (String)null, 2, (Object)null).build();
MapView var18 = this.mapView;
Intrinsics.checkNotNull(var18);
Style var19 = var18.getMapboxMap().getStyle();
if (var19 != null) {
//This is the part not working and I can't find a method for
SourceUtils.addSource((StyleInterface)var19, (StyleContract.StyleSourceExtension)item);
}
FillLayer polygonLayer = new FillLayer(id + "FillLayer_ID", id);
polygonLayer.fillColor(fillColor);
polygonLayer.fillOpacity(Opacity);
SymbolLayer symbolLayer = new SymbolLayer(id + "SymbolLAYER_ID", id);
symbolLayer.textField(id);
symbolLayer.textSize(fontSize);
symbolLayer.textColor(fillColor);
symbolLayer.textOpacity(1.0);
try {
var18 = this.mapView;
Intrinsics.checkNotNull(var18);
var19 = var18.getMapboxMap().getStyle();
if (var19 != null) {
LayerUtils.addPersistentLayer$default((StyleInterface)var19, (Layer)polygonLayer, (LayerPosition)null, 2, (Object)null);
}
var18 = this.mapView;
Intrinsics.checkNotNull(var18);
var19 = var18.getMapboxMap().getStyle();
if (var19 != null) {
LayerUtils.addPersistentLayer$default((StyleInterface)var19, (Layer)symbolLayer, (LayerPosition)null, 2, (Object)null);
}
} catch (Exception var14) {
String var20 = var14.getMessage();
Intrinsics.checkNotNull(var20);
Log.d("removeAnnotation", var20);
}
}
The issue is the line:
SourceUtils.addSource((StyleInterface)var19, (StyleContract.StyleSourceExtension)item);
I've not been able to find a working example or method for it. The only java example that's available from mapbox is from the v9 sdk and doesn't work on the new version of the sdk.
Solved. Line should actually be:
item.bindTo(var19);
Also need to wait until the map style is fully loaded before you can add the element.