I am using the ArcGIS Runtime SDK for Java in my project. I need to add latitude/longitude values for GPS tracking. I can't find a solution to perform this. There are sample codes to perform this, but I think they are using a CSV file for the values.
I can't find a way to add a path of the CSV file or add a CSV file.
Sample code:
private static final String FSP = System.getProperty("file.separator");
private String getPathSampleData() {
String dataPath = null;
String javaPath = ArcGISRuntime.getInstallDirectory();
if (javaPath != null) {
if (!(javaPath.endsWith("/") || javaPath.endsWith("\\"))) {
javaPath += FSP;
}
dataPath = javaPath + "sdk" + FSP + "samples" + FSP + "data" + FSP;
}
File dataFile = new File(dataPath);
if (!dataFile.exists()) {
dataPath = ".." + FSP + "data" + FSP;
}
return dataPath;
}
Can any one help me to solve my issue?
You mentioned both GPS and CSV, and I will explain how to use both of them in ArcGIS Runtime.
Use the GPSLayer
class to display a location stream on the map. There are a few options for using GPSLayer
:
SerialPortGPSWatcher
to connect to a serial port (or virtual serial port) GPS (sample).FileGPSWatcher
to use an NMEA file of GPS points (sample 1, sample 2).IGPSWatcher
interface to do something other than real GPS or an NMEA file, such as a CSV file.If what you really want is to display a CSV on the map, I can think of two options:
GraphicsLayer
(sample). (There are 1,001 ways to parse a CSV in Java; if I were writing that code, BufferedReader
and StringTokenizer
would be my friends.)Use the CSVLayer
class. There aren't any samples and it's not very well documented, because it appears to have been written for internal use for displaying a CSV layer stored in a web map. But you can use it with a CSV file as follows:
//map is of type JMap
map.addMapEventListener(new MapEventListenerAdapter() {
@Override
public void mapReady(MapEvent event) {
String layerDef = "{ \"geometryType\": \"esriGeometryPoint\", \"type\": \"Feature Layer\", \"typeIdField\": \"\", \"drawingInfo\": { \"renderer\": { \"type\": \"simple\", \"symbol\": { \"type\": \"esriSMS\", \"style\": \"esriSMSCircle\", \"color\": [200, 40, 0, 255], \"size\": 15, \"angle\": 0, \"xoffset\": 0, \"yoffset\": 0 } }, \"fixedSymbols\": true }, \"fields\": [ { \"name\": \"X\", \"alias\": \"X\", \"type\": \"esriFieldTypeDouble\", \"editable\": true, \"nullable\": true, \"domain\": null }, { \"name\": \"Y\", \"alias\": \"Y\", \"type\": \"esriFieldTypeDouble\", \"editable\": true, \"nullable\": true, \"domain\": null } ], \"name\": \"My CSV Layer\" }";
CSVLayer.CSVConfig csvConfig = new CSVLayer.CSVConfig();
csvConfig.url = new File("/path/to/csv-file.csv").toURI().toString();
csvConfig.columnDelimiter = ",";
csvConfig.longitudeField = "X";
csvConfig.latitudeField = "Y";
try {
final CSVLayer csvLayer = new CSVLayer(layerDef, csvConfig);
map.getLayers().add(csvLayer);
} catch (Exception ex) {
//Handle the exception
}
}
});
Unfortunately, you do need that JSON string in order to use CSVLayer
. I made the JSON string as simple as possible. You'll need to adjust it if your field names for longitude and latitude are not X
and Y
. You can see the layerDefinition
schema documentation and a more elaborate example (description, JSON).
The JSON string you need for option 2 makes option 1 the better choice in my opinion. Parsing a CSV is easy; managing that JSON string is harder.