Reading Latitude And Longitude From A Text File
I currently have an application hard coded with lat and long positions. Instead I want to read the lat and long from a text file called cities.text which contains the following Dub
Solution 1:
Assuming you have this file in your assets folder with the lat-lng data in the above format, you can use this function
private HashMap<String, Location> readFile(String path){
HashMap<String, Location> citiesLoactionMap = new HashMap<>();
String dump = null;
try {
dump = readFromAssets(mContext, path);
} catch (IOException e) {
e.printStackTrace();
}
if (dump != null && !dump.isEmpty()) {
String[] cities = dump.split("\n");
for(String city : cities){
String[] cityData = city.split("\\s+");
if(cityData[0] != null && cityData[1] != null && cityData[2] != null){
Location location = new Location(cityData[0]);
location.setLatitude(Double.parseDouble(cityData[1]));
location.setLongitude(Double.parseDouble(cityData[2]));
citiesLoactionMap.put(cityData[0], location);
}
}
}
return citiesLoactionMap;
}
Here path would be the the file name, e.g. "cities.txt"
Post a Comment for "Reading Latitude And Longitude From A Text File"