I am having two issues with my application. The first one is that i am trying to read a column from a CSV file that has empty values. The second one is that based on the values read from the CSV i should change the colours of the markers uploaded on the google map, but right now the colour changed based on the last value read and all the markers get the same colour and not a colour based on their corresponded value. So my questions are: how can i stop the app from crashing when it gets to the empty value from CSV and do i change the colour of each marker based on their corresponded value?
The CSV file:
1,45.66680458,25.60458787,salvat
2,45.66672655,25.6047773,gresit
3,45.66670734,25.60465392,
Updated and working code:
public void Upload() throws IOException {
File file = new File(getExternalFilesDir(null), "fisier.csv");
if (file.exists()) {
InputStream instream = new FileInputStream(file);
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader reader = new BufferedReader(inputreader);
List<LatLng> latLngList = new ArrayList<LatLng>();
String line = " ";
List<String> stares = new ArrayList<String>();
while ((line = reader.readLine()) != null) // Read until end of file
{
String[] tokens = line.split(",", 4);
double lat = Double.parseDouble(tokens[1]);
double lon = Double.parseDouble(tokens[2]);
stare = String.valueOf(tokens[3]);
latLngList.add(new LatLng(lat, lon));
stares.add(stare);
mMap.setOnMarkerClickListener(this);
}
reader.close();
for (int i = 0; i < latLngList.size(); i++) {
float color = 0;
if (stares.get(i).equals("gresit")) {
color = BitmapDescriptorFactory.HUE_BLUE;
}
else if (stares.get(i).equals("salvat")) {
color = BitmapDescriptorFactory.HUE_GREEN;
}
else {
color = BitmapDescriptorFactory.HUE_RED;
}
mMap.addMarker(new MarkerOptions()
.position(latLngList.get(i))
.title("Din CSV!")
.draggable(true)
.icon(BitmapDescriptorFactory.defaultMarker(color)));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLngList.get(i), DEFAULT_ZOOM));
}
Toast.makeText(MapsActivity.this,"Fisierul a fost incarcat", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MapsActivity.this,"Nu exista fisierul!", Toast.LENGTH_SHORT).show();
}
}
Define another list for the stare values:
List<String> stares = new ArrayList<String>();
Inside the while loop after latLngList.add(new LatLng(lat, lon)); add this:
stares.add(stare);
Change the for loop:
for (int i = 0; i < latLngList.size(); i++) {
if (stares.get(i).equals("gresit")) {
color = BitmapDescriptorFactory.HUE_BLUE;
} else if (stares.get(i).equals("salvat")) {
color = BitmapDescriptorFactory.HUE_GREEN;
} else {
color = BitmapDescriptorFactory.HUE_RED;
}
mMap.addMarker(new MarkerOptions()
.position(latLngList.get(i))
.title("Din CSV!")
.draggable(true)
.icon(BitmapDescriptorFactory.defaultMarker(color))
};
Update:
replace stare = String.valueOf(tokens[3]); with:
try {
stare = String.valueOf(tokens[3]);
} catch (Exception e) {
stare = "";
e.printStackTrace();
}
so the app does not crash if there is no 4th column.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With