Mapbox:如何从 Mapbox 定义的片段中获取 MapView 的引用?
Mapbox: How to get a reference of a MapView from the Mapbox defined Fragment?
我在这里按照 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
检查它是否为 null 以防止 NullPointerExceptions 也很好。
我在这里按照 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
检查它是否为 null 以防止 NullPointerExceptions 也很好。