我将SelectBuildingActivity from the official MapBox repository分解为一个非常基本的示例,在该示例中,我想从预定义点绘制建筑物:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private MapView mapView;
private LatLng buildingPoint = new LatLng(37.791298, -122.396388);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));
// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_main);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.LIGHT, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
style.addSource(new GeoJsonSource("source-id"));
style.addLayer(new FillLayer("layer-id", "source-id").withProperties(
PropertyFactory.fillColor(Color.parseColor("#8A8ACB"))
));
final PointF finalPoint = mapboxMap.getProjection().toScreenLocation(buildingPoint);
//TODO how can we check if everything is rendered and ready to use?
List<Feature> features = mapboxMap.queryRenderedFeatures(finalPoint, "building");
if (features.size() > 0) {
GeoJsonSource selectedBuildingSource = style.getSourceAs("source-id");
if (selectedBuildingSource != null) {
selectedBuildingSource.setGeoJson(FeatureCollection.fromFeatures(features));
}
}
}
});
}
//Overridden lifecycle methods would be here
}
现在mapboxMap.queryRenderedFeatures(finalPoint, "building")
返回一个空列表。从文档中:
如果地图或基础渲染表面已被破坏,则返回一个空列表。
那么我们如何确定所有必需的东西都已经加载并呈现?我确保正确加载了地图和样式。缺少哪一步?还有其他回调吗?