I'm working with Google Maps and want to read in location objects into the map. I'm loading them in from a .csv file and create Shelter objects for each location that I save in an ArrayList. To actually add them to the map I iterate through this ArrayList and create marker objects with each object's data. Each actual Marker object created is also saved into another ArrayList so that I can reference them later (show/hide them on map when wanted). I feel like I'm doing something wrong. I included the relevant methods below.
private ArrayList<ShelterObject> shelterObjects;
private ArrayList<Marker> shelterMarkers;
// Generates the Shelter objects from the csv file and adds them to the ArrayList.
private void generateShelterObjects()
{
shelterObjects = new ArrayList<>();
Log.i("SHELTER_TEST", "METHOD generateShelterObjects() STARTED");
InputStream is = getResources().openRawResource(R.raw.shelters_csv_file);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8")));
String line = "";
try {
while ((line = reader.readLine()) != null) {
// Split the line into different tokens (using the comma as a separator).
String[] tokens = line.split(",");
String address = tokens[0];
double latitude = Double.parseDouble(tokens[1]);
double longitude = Double.parseDouble(tokens[2]);
int numberOfOccupants = Integer.parseInt(tokens[3]);
ShelterObject object = new ShelterObject(address, latitude, longitude, numberOfOccupants);
shelterObjects.add(object);
}
} catch (IOException e1) {
Log.e("MapsActivity2", "Error" + line, e1);
e1.printStackTrace();
}
Log.i("SHELTER_TEST", "The size of the arraylist after reading in objects: " + shelterObjects.size());
}
// Iterates through the ArrayList of Shelter objects and adds a marker with data from that object to the map. The markers are saved in another ArrayList so they can be referenced later.
private void addShelterMarkersToMap()
{
shelterMarkers = new ArrayList<>();
for (ShelterObject obj : shelterObjects) {
LatLng latLng = new LatLng(obj.getLatitude(), obj.getLongitude());
Marker marker = mMap.addMarker(new MarkerOptions()
.position(latLng)
.title(obj.getAddress())
.snippet("Antal platser: " + obj.getNumberOfOccupants()));
shelterMarkers.add(marker);
}
}