I managed to fetch long and lat values from CSV and loop them to display all values in google map .Now i need to filter column Fuel_Type = "Unleaded 91" how to add this to while loop ? while( (line = reader.readLine()) != null)
here is my code to fetch data from CSV
private void readDataFromCSV() {
// Read the raw csv file
InputStream is = getResources().openRawResource(R.raw.data);
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, Charset.forName("UTF-8"))
);
boolean header = true;
List<String> list = new ArrayList<>();
List<LatLng> latLngList = new ArrayList<LatLng>();
// Initialization
try {
reader.readLine();
while( (line = reader.readLine()) != null) // Read until end of file
{
double lat = Double.parseDouble(line.split(",")[7]);
double lon = Double.parseDouble(line.split(",")[8]);
latLngList.add(new LatLng(lat, lon));
}
// Add them to map
for(LatLng pos : latLngList)
{
mMap.addMarker(new MarkerOptions().position(pos).title("title").icon(bitmapDescriptorFromVector(getApplicationContext(), R.drawable.seveneleven)));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(pos, 18f));
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(MapsActivity.this));
}
} catch (IOException e) {
Log.wtf("MapsActivity", "Error reading data file on line" line, e);
e.printStackTrace();
}
}
my CSV file :
SiteId,Site_Name,Site_Brand,Sites_Address_Line_1,Site_Suburb,Site_State,Site_Post_Code,Site_Latitude,Site_Longitude,Fuel_Type,Price,TransactionDateutc
61291313,7-Eleven Runaway Bay,BP,20 Bayview Street,Mungindi,QLD,4497,-27.923529,153.403729,Unleaded 91,1840,1/3/2022 0:00
61291313,Caltex Labrador,BP,150 Brisbane Road,Mungindi,QLD,4497,-27.924007,153.403869,Diesel,1860,2/3/2022 21:49
61291313,Coles shell,BP,69 Frank Street,Mungindi,QLD,4497,-27.923863,153.403528,Diesel,1900,3/3/2022 22:21
61291313,BP Shop Helensvale,BP,2 Discovery Drive,Mungindi,QLD,4497,-27.923655,153.403608,Diesel,2150,9/3/2022 23:12
CodePudding user response:
you can check if line
contains your fuel type or not, like this:
while( (line = reader.readLine()) != null) // Read until end of file
{
if(line.contains("YOUR_FUEL_TYPE"){
double lat = Double.parseDouble(line.split(",")[7]);
double lon = Double.parseDouble(line.split(",")[8]);
latLngList.add(new LatLng(lat, long));
}
}