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?
Asked
Active
Viewed 1,465 times
2 Answers
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
-
This is an excellent help, thank you! Just to work out how to write a LatLng csv file now. Is it really as simple as your example? – noppoo Jul 22 '14 at 16:21
-
Is what as simple as my example? Reading the file? – crocboy Jul 22 '14 at 16:37
-
the nature of the csv file – noppoo Jul 22 '14 at 17:09
-
Yes it is - it simply means 'comma-seperated-value', and in your case you only need two values. It is very easy to parse these files using Java's `split()` method – crocboy Jul 22 '14 at 17:17
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