2

I'm looking to lay markers of around 60 LatLngs to a google map fragment. (They will be clustered, but that's another topic I think). I don't want to hardcode 60 variables, so I think storing them in a csv file and plotting them from there is probably better. How would I do this?

noppoo
  • 45
  • 6

2 Answers2

1

Your CSV file might look like this:

48.45126,96.45631
46.56541,95.65476
47.56723,94.58639

You can store the file in the res folder, or you can store it on internal storage. If you store your file in res, you will never be able to change it, as it is packaged with the APK and read-only. See this for more details. Either way, get your file:

InputStream instream = new FileInputStream("location_of_file.csv");
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader reader= new BufferedReader(inputreader);
List<LatLng> latLngList = new ArrayList<LatLng>();
String line = "";

while( (line = reader.readLine()) != null) // Read until end of file
{
  double lat = Double.parseDouble(line.split(",")[0]);
  double lon = Double.parseDouble(line.split(",")[1]);
  latLngList.add(new LatLng(lat, lon));
}

// Add them to map
for(LatLng pos : latLngList)
{
  mMap.addMarker(new MarkerOptions()
        .position(pos)
        .title("Title!")); // Don't necessarily need title
}

Hope this helped!

crocboy
  • 2,887
  • 2
  • 22
  • 25
0

Step 1 Add your csv to your Assets folder

Step 2 Read in your csv file like

BufferedReader reader = new BufferedReader(new InputStreamReader(getAssets().open("test.csv")));

Step 3 Parse your csv file

 String line;
    while ((line = reader.readLine()) != null) {
         String[] RowData = line.split(",");
         date = RowData[0];
         value = RowData[1];
        // do something with "data" and "value"
    }

Check out

Get and Parse CSV file in android

How to parse the CSV file in android application?

Community
  • 1
  • 1
AmmarCSE
  • 30,079
  • 5
  • 45
  • 53