Hi All, I am very new in ArcGIS Runtime SDK for Android 100.2.0. Android studio 3 platform is used to load a local shapefile in my android mobile,but nothing shown. MainActivity.java as below. Any idea?
package com.example.interfly.displaymap;
import android.graphics.Color;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.esri.arcgisruntime.data.ShapefileFeatureTable;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.Basemap;
import com.esri.arcgisruntime.mapping.LayerList;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.Renderer;
import com.esri.arcgisruntime.symbology.SimpleFillSymbol;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;
import com.esri.arcgisruntime.symbology.Symbol;
import java.io.File;
import static android.media.MediaCodec.MetricsConstants.MODE;
public class MainActivity extends AppCompatActivity {
private MapView mMapView;
@Override
protected void onPause(){
mMapView.pause();
super.onPause();
}
@Override
protected void onResume(){
super.onResume();
mMapView.resume();
}
public String getSDPath(){
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED); // sd card exist?
if (sdCardExist)
{
sdDir = Environment.getExternalStorageDirectory();// get root dir.
}
return sdDir.toString();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMapView = findViewById(R.id.mapView);
ArcGISMap map = new ArcGISMap();
//ArcGISMap map = new ArcGISMap(Basemap.createStreetsVector());
String shpPath=getSDPath()+ "/arcgis/shapefile/GeoNet_cmt.shp";
ShapefileFeatureTable shapefileFeatureTable_geo = new ShapefileFeatureTable(shpPath);
FeatureLayer featureLayer = new FeatureLayer(shapefileFeatureTable_geo);
featureLayer.setRenderer(new SimpleRenderer(new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 10)));
map.getOperationalLayers().add(featureLayer);
mMapView.setMap(map);
}
}
Solved! Go to Solution.
Hi Wu,
I tested with the same code snippet I gave you in my previous post and I was able to load and display the shapefile on the map; see attached snapshot
below is the code snippet I used.
private void loadShapeFileAndDisplay(String location){ ShapefileFeatureTable featureTable = new ShapefileFeatureTable(location); featureTable.loadAsync(); featureTable.addDoneLoadingListener(()->{ if(featureTable.getLoadStatus() == LoadStatus.LOADED){ FeatureLayer featureLayer = new FeatureLayer(featureTable); featureLayer.loadAsync(); mMapView.getMap().getOperationalLayers().add(featureLayer); }else Log.e("Load Error: ", featureTable.getLoadError().getAdditionalMessage()); }); }
I hope this helps in resolving your issue. Let me know if you run into further problems.
Hi Wu,
You need to call loadAsync() on the FeatureLayer before adding it to the map. So make the following correction in your code.
FeatureLayer featureLayer = new FeatureLayer(shapefileFeatureTable_geo);
featureLayer.loadAsync(); //needed correction
featureLayer.setRenderer(new SimpleRenderer(new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 10)));
map.getOperationalLayers().add(featureLayer);
The FeatureLayer class abstracts a loadable resource and hence implements the Loadable interface. Please have a look at the guide on the Loadable pattern for asynchronous resources—ArcGIS Runtime SDK for Android | ArcGIS for Developers
Hi Asante, Thank you very much for your quick reply. I add the "featureLayer.loadAsync();" but the screen still keep blank. I replaced "ArcGISMap map = new ArcGISMap();" with "ArcGISMap map = new ArcGISMap(Basemap.createStreetsVector()); " , the app only show basemap with no my shapefile points.
Hi Wu,
Please use the following code snippet to check the state of the ShapefileFeatureTable object reference and report any errors you might see. If you are unable to implement it please share your data if that's possible. That will allow me to investigate a bit further to give you better feedback.
ShapefileFeatureTable shapefile = new ShapefileFeatureTable(PATH_TO_SHAPEFILE);
shapefile.addDoneLoadingListener(()->{
if(shapefile.getLoadStatus() == LoadStatus.NOT_LOADED || shapefile.getLoadStatus() == LoadStatus.FAILED_TO_LOAD){
System.out.println("Unable to load shapefile due to: "+ shapefile.getLoadError().getAdditionalMessage());
shapefile.retryLoadAsync(); // retry the loading operation
}
});
Hi Asante, the studio give the message " lamda expressions are not supported at this language level " , so I failed to implement the code above. The shapefile had been shared as a attachment in my original post.
Hello Wu,
you can add the following to your build.gradle file (within the android block) to allow for supporting lambda expressions in your android code.
compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 }
I will test with your data soon and let you know what I find.
Kwasi
Hi, I am very new to ArcGIS runtime, and I would like to ask what is the format of the file path. For I have written codes as shown below:
And shapefile feature table failed to load. Could you help me?
Hi Wu,
I tested with the same code snippet I gave you in my previous post and I was able to load and display the shapefile on the map; see attached snapshot
below is the code snippet I used.
private void loadShapeFileAndDisplay(String location){ ShapefileFeatureTable featureTable = new ShapefileFeatureTable(location); featureTable.loadAsync(); featureTable.addDoneLoadingListener(()->{ if(featureTable.getLoadStatus() == LoadStatus.LOADED){ FeatureLayer featureLayer = new FeatureLayer(featureTable); featureLayer.loadAsync(); mMapView.getMap().getOperationalLayers().add(featureLayer); }else Log.e("Load Error: ", featureTable.getLoadError().getAdditionalMessage()); }); }
I hope this helps in resolving your issue. Let me know if you run into further problems.
Hi Asante, Yes, that fix my problem. Thank you very much for your patient instructions. My job is related with field survey. We taken a laptop with a GPS sensor in field which can show geological map in real time that greatly helpful for our job, but not so handy. Now I would like use my android mobile phone to do that job, so the next step is GPS function, inquiry and so on, that is a little difficult for me. I wonder whether Esri has a android version of ArcMap, if not, do you have that kind of code sample, and more importantly, where can I find systematical education materials for me. I am a little familiar with ArcPy but blank in JAVA and android SDK.
Hi Wu,
You are welcome; I am glad the snippet was helpful in achieving your goal. As far as field work is concerned you have the option of developing your own app with the ArcGIS Runtime Android SDK (that will require some android/java development skills/knowledge). A better option for non-developers is using one of Esri excellent apps. For the tasks you have described I will recommended Collector for ArcGIS (Collector for ArcGIS - Collect & Update Data in the Field | Esri ). I am roping in on Philip Mcneilly; he is better placed than I am to give you the right information on Collector.
If you are still interested in pursuing the development of your own application, I will advise you log a case with Technical Support and we will guide you in using the runtime SDKs.
Regards