我設法從 CSV 中獲取 long 和 lat 值并回圈它們以顯示谷歌地圖中的所有值。現在我需要過濾列 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
uj5u.com熱心網友回復:
您可以檢查是否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));
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/474623.html
上一篇:Go 簡單入門
