Guest User

Untitled

a guest
Feb 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. private static final String TAG = MainActivity.class.getSimpleName();
  2.  
  3. // permission to read external storage
  4. private final String[] reqPermission = new String[] { Manifest.permission.READ_EXTERNAL_STORAGE };
  5.  
  6. private MapView mMapView;
  7.  
  8.  
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_main);
  14.  
  15. // create map and add to map view
  16. //ArcGISMap map = new ArcGISMap(Basemap.createStreets());
  17. //mMapView = findViewById(R.id.mapView);
  18. // mMapView.setMap(map);
  19.  
  20. // For API level 23+ request permission at runtime
  21. if (ContextCompat.checkSelfPermission(MainActivity.this, reqPermission[0]) == PackageManager.PERMISSION_GRANTED) {
  22. LoadGeodatabase loadGeodatabase = new LoadGeodatabase();
  23. loadGeodatabase.execute();
  24. } else {
  25. // request permission
  26. int requestCode = 2;
  27. ActivityCompat.requestPermissions(MainActivity.this, reqPermission, requestCode);
  28. }
  29. }
  30.  
  31. @Override
  32. protected Void doInBackground(Void... params){
  33.  
  34. // create path to local geodatabase
  35. String path = Environment.getExternalStorageDirectory() + getString(R.string.config_data_sdcard_offline_dir)
  36. + getString(R.string.config_geodb_name);
  37.  
  38.  
  39. // create a new geodatabase from local path
  40. final Geodatabase geodatabase = new Geodatabase(path);
  41. geodatabase.loadAsync();
  42. //Takes each layer one by one from the Geodatabase and adds it to the MapView
  43.  
  44. geodatabase.addDoneLoadingListener(() -> {
  45. if (geodatabase.getLoadStatus() == LoadStatus.LOADED) {
  46.  
  47. for (int i = (geodatabase.getGeodatabaseFeatureTables().size()) - 1; i >= 0; i--) {
  48. GeodatabaseFeatureTable geodatabaseFeatureTable = geodatabase.getGeodatabaseFeatureTableByServiceLayerId(i);
  49.  
  50. final FeatureLayer featureLayer = new FeatureLayer(geodatabaseFeatureTable);
  51.  
  52. featureLayer.addDoneLoadingListener(() -> {
  53. if (featureLayer.getLoadStatus() == LoadStatus.LOADED) {
  54. // set viewpoint to the feature layer's extent
  55. mMapView.setViewpointAsync(new Viewpoint(featureLayer.getFullExtent()));
  56. } else {
  57. Toast.makeText(MainActivity.this, "Feature Layer failed to load!", Toast.LENGTH_LONG).show();
  58. Log.e(TAG, "Feature Layer failed to load!");
  59. }
  60. });
  61. // add feature layer to the map
  62. mMapView.getMap().getOperationalLayers().add(featureLayer);
  63. }
  64.  
  65.  
  66. } else {
  67. Toast.makeText(MainActivity.this, "Geodatabase failed to load!", Toast.LENGTH_LONG).show();
  68. Log.e(TAG, "Geodatabase failed to load!");
  69. }
  70. });
  71.  
  72.  
  73. return null;
  74. }
  75.  
  76. /**
  77. * Handle the permissions request response
  78. */
  79. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  80. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  81. LoadGeodatabase loadGeodatabase = new LoadGeodatabase();
  82. loadGeodatabase.execute();
  83. } else {
  84. // report to user that permission was denied
  85. Toast.makeText(MainActivity.this, getResources().getString(R.string.location_permission_denied),
  86. Toast.LENGTH_SHORT).show();
  87. }
  88. }
  89.  
  90. @Override
  91. protected void onPause() {
  92. super.onPause();
  93. mMapView.pause();
  94. }
  95.  
  96. @Override
  97. protected void onResume() {
  98. super.onResume();
  99. mMapView.resume();
  100. }
Add Comment
Please, Sign In to add comment