从 CSV 文件中读取经纬度并在对列应用过滤后显示在 google 地图中
Reading Latitude and longitude from CSV file and display in google map after applying filtering to a column
我设法从 CSV 中获取 long 和 lat 值并循环显示 google 地图中的所有值。现在我需要过滤列 Fuel_Type = "Unleaded 91" 如何添加它到 while 循环?
while( (line = reader.readLine()) != null)
这是我从 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();
}
}
我的 CSV 文件:
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
您可以检查 line
是否包含您的燃料类型,如下所示:
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));
}
}
我设法从 CSV 中获取 long 和 lat 值并循环显示 google 地图中的所有值。现在我需要过滤列 Fuel_Type = "Unleaded 91" 如何添加它到 while 循环? while( (line = reader.readLine()) != null)
这是我从 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();
}
}
我的 CSV 文件:
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
您可以检查 line
是否包含您的燃料类型,如下所示:
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));
}
}