我正在尝试使用Mapbox Android SDK将圆形标记放置在GeoJSON形状的上方。当我运行以下代码时,我只会看到GeoJSON形状(我认为圆圈是在GeoJSON下方绘制的)。如果我将try / catch块放在圆圈代码上方,则应用程序崩溃。如何在GeoJSON形状上方绘制圆? (我计划在GeoJSON形状的顶部绘制许多标记)
我的尝试(成功绘制了geojson形状,但没有圆)
mapboxMap.setStyle(Style.LIGHT, style -> {
circleManager = new CircleManager(mapView, mapboxMap, style);
CircleOptions circleOptions = new CircleOptions()
.withLatLng(new LatLng(0.0, 0.0))
.withCircleColor(ColorUtils.colorToRgbaString(Color.RED))
.withCircleRadius(12f)
.setDraggable(false);
circleManager.create(circleOptions);
try {
GeoJsonSource source = new GeoJsonSource("source-id", loadJsonFromAsset("building.geojson"));
FillLayer fillLayer = new FillLayer("layer-id", "source-id");
fillLayer.setProperties(
PropertyFactory.fillColor(Color.parseColor("#3bb2d0")),
PropertyFactory.fillOutlineColor(Color.BLACK));
mapboxMap.setStyle(new Style.Builder().withSource(source).withLayer(fillLayer));
} catch (Exception e) {
Log.e("failure", "failed to set add map layer", e);
}
});
我的第二次尝试(这会导致应用崩溃-见下文)
mapboxMap.setStyle(Style.LIGHT, style -> {
try {
GeoJsonSource source = new GeoJsonSource("source-id", loadJsonFromAsset("building.geojson"));
FillLayer fillLayer = new FillLayer("layer-id", "source-id");
fillLayer.setProperties(
PropertyFactory.fillColor(Color.parseColor("#3bb2d0")),
PropertyFactory.fillOutlineColor(Color.BLACK));
mapboxMap.setStyle(new Style.Builder().withSource(source).withLayer(fillLayer));
} catch (Exception e) {
Log.e("failure", "failed to set add map layer", e);
}
circleManager = new CircleManager(mapView, mapboxMap, style);
CircleOptions circleOptions = new CircleOptions()
.withLatLng(new LatLng(0.0, 0.0))
.withCircleColor(ColorUtils.colorToRgbaString(Color.RED))
.withCircleRadius(12f)
.setDraggable(false);
circleManager.create(circleOptions);
});
崩溃输出
2019-01-17 14:32:14.087 27217-27217/com.nocompany.mapboxdemo E/Mbgl-MapChangeReceiver: Exception in onDidFinishLoadingStyle
java.lang.RuntimeException: The style has to be non-null and fully loaded.
at com.mapbox.mapboxsdk.plugins.annotation.AnnotationManager.<init>(AnnotationManager.java:77)
at com.mapbox.mapboxsdk.plugins.annotation.CircleManager.<init>(CircleManager.java:79)
at com.mapbox.mapboxsdk.plugins.annotation.CircleManager.<init>(CircleManager.java:56)
at com.mapbox.mapboxsdk.plugins.annotation.CircleManager.<init>(CircleManager.java:44)
at com.nocompany.mapboxdemo.MainActivity$1.lambda$onMapReady$0(MainActivity.java:116)
at com.nocompany.mapboxdemo.-$$Lambda$MainActivity$1$WjHCwDcEHlUWOHE7h-69Jk43tfY.onStyleLoaded(Unknown Source:4)
at com.mapbox.mapboxsdk.maps.MapboxMap.notifyStyleLoaded(MapboxMap.java:835)
at com.mapbox.mapboxsdk.maps.MapboxMap.onFinishLoadingStyle(MapboxMap.java:202)
at com.mapbox.mapboxsdk.maps.MapView$MapCallback.onDidFinishLoadingStyle(MapView.java:1152)
at com.mapbox.mapboxsdk.maps.MapChangeReceiver.onDidFinishLoadingStyle(MapChangeReceiver.java:194)
at com.mapbox.mapboxsdk.maps.NativeMapView.onDidFinishLoadingStyle(NativeMapView.java:979)
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:325)
at android.os.Looper.loop(Looper.java:142)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
我正在使用MapBox SDK 7.0.0
谢谢!