Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final String TAG = MainActivity.class.getSimpleName();
- // permission to read external storage
- private final String[] reqPermission = new String[] { Manifest.permission.READ_EXTERNAL_STORAGE };
- private MapView mMapView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // create map and add to map view
- //ArcGISMap map = new ArcGISMap(Basemap.createStreets());
- //mMapView = findViewById(R.id.mapView);
- // mMapView.setMap(map);
- // For API level 23+ request permission at runtime
- if (ContextCompat.checkSelfPermission(MainActivity.this, reqPermission[0]) == PackageManager.PERMISSION_GRANTED) {
- LoadGeodatabase loadGeodatabase = new LoadGeodatabase();
- loadGeodatabase.execute();
- } else {
- // request permission
- int requestCode = 2;
- ActivityCompat.requestPermissions(MainActivity.this, reqPermission, requestCode);
- }
- }
- @Override
- protected Void doInBackground(Void... params){
- // create path to local geodatabase
- String path = Environment.getExternalStorageDirectory() + getString(R.string.config_data_sdcard_offline_dir)
- + getString(R.string.config_geodb_name);
- // create a new geodatabase from local path
- final Geodatabase geodatabase = new Geodatabase(path);
- geodatabase.loadAsync();
- //Takes each layer one by one from the Geodatabase and adds it to the MapView
- geodatabase.addDoneLoadingListener(() -> {
- if (geodatabase.getLoadStatus() == LoadStatus.LOADED) {
- for (int i = (geodatabase.getGeodatabaseFeatureTables().size()) - 1; i >= 0; i--) {
- GeodatabaseFeatureTable geodatabaseFeatureTable = geodatabase.getGeodatabaseFeatureTableByServiceLayerId(i);
- final FeatureLayer featureLayer = new FeatureLayer(geodatabaseFeatureTable);
- featureLayer.addDoneLoadingListener(() -> {
- if (featureLayer.getLoadStatus() == LoadStatus.LOADED) {
- // set viewpoint to the feature layer's extent
- mMapView.setViewpointAsync(new Viewpoint(featureLayer.getFullExtent()));
- } else {
- Toast.makeText(MainActivity.this, "Feature Layer failed to load!", Toast.LENGTH_LONG).show();
- Log.e(TAG, "Feature Layer failed to load!");
- }
- });
- // add feature layer to the map
- mMapView.getMap().getOperationalLayers().add(featureLayer);
- }
- } else {
- Toast.makeText(MainActivity.this, "Geodatabase failed to load!", Toast.LENGTH_LONG).show();
- Log.e(TAG, "Geodatabase failed to load!");
- }
- });
- return null;
- }
- /**
- * Handle the permissions request response
- */
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- LoadGeodatabase loadGeodatabase = new LoadGeodatabase();
- loadGeodatabase.execute();
- } else {
- // report to user that permission was denied
- Toast.makeText(MainActivity.this, getResources().getString(R.string.location_permission_denied),
- Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- protected void onPause() {
- super.onPause();
- mMapView.pause();
- }
- @Override
- protected void onResume() {
- super.onResume();
- mMapView.resume();
- }
Add Comment
Please, Sign In to add comment