Mapbox:如何从Mapbox定义的片段中获得MapView的引用



我在这里遵循了Mapbox的说明https://docs.mapbox.com/android/maps/examples/support-map-fragment/我可以成功地从MapFragment中可视化地图。

//Create the mapFragment
if (savedInstanceState == null) {
// Create fragment
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Build mapboxMap
MapboxMapOptions options = MapboxMapOptions.createFromAttributes(this, null).doubleTapGesturesEnabled(true);
options.camera(new CameraPosition.Builder()
.target(new LatLng(-52.6885, -70.1395))
.zoom(14)
.build());
// Create map fragment
mapFragment = SupportMapFragment.newInstance(options);
// Add map fragment to parent container
transaction.add(R.id.container, mapFragment, "com.mapbox.map");
transaction.commit();
} else {
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag("com.mapbox.map");        
// Working with Fragment - getFragment by ID
}
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}

现在我想介绍LineManager,以便在注释插件的帮助下定义一些行,可以在这里找到https://docs.mapbox.com/android/plugins/overview/annotation/但问题是,我不能初始化它,因为这个部分:

LineManager lineManager = new LineManager(mapView, mapboxMap, style);

我有mapboxMap和样式,但我没有mapView。

那么,我们如何从Mapbox定义的MapFragment中获取mapView呢?

我找到了。对于以后想使用这种方法的人,你需要调用

mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag("com.mapbox.map");
(MapView) mapFragment.getView();  // Might return null

为了防止NullPointerExceptions,最好检查它是否为null。

最新更新