该方法即将转换来自另一个从文件读取的方法的字符串。此方法的目标是在谷歌地图上绘制一些点(从文件中读取)。我试图从readTheFile()
分割我的字符串,我得到这样的内容:latLng[0] = 68.43752735570531,17.433074489235878
和latLng[1] = 68.43879697044444,17.436143271625042
。字符串:latLngString = "68.43752735570531,17.433074489235878\n68.43879697044444,17.436143271625042\n"
。问题是我根本无法得到适当的分裂?或者这是最好的方法吗? - 我的两种方法: - 任何帮助都非常感谢 - 谢谢 -
从文件中读取的方法:
private String readFromFile() {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
File file = new File(path, fileName);
String line = "";
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
text.append(line); // + '\n');
text.append("\n");
}
br.close();
} catch (IOException e) {
Log.w("I/O exception" + file, e);
}
return text.toString();
}
从文件中提取点数:
private void drawPointsFromFile() {
String latLngString = readFromFile();
String[] latLng = latLngString.split("\n", 2);
for (int i = 0; i < latLng.length; i++) {
Double latitude = Double.parseDouble(latLng[0]), longitude = Double.parseDouble(latLng[1]);
map = ((MapFragment) this.getFragmentManager().findFragmentById(R.id.map)).getMap();
map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title("Lat: " + latitude + "," + " Lng: " + longitude)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.myicon)));
}
}
答案 0 :(得分:0)
你可以试试这个,
String string = "68.43752735570531,17.433074489235878\n68.43879697044444,17.436143271625042\n";
// replace "\n" with ","
String replacedString = string.replace("\n", ",");
//split by ","
String points[]= replacedString.split(",");
//every odd indexs will be lats and even indexes will be longs
然后你可以将lats和longs分成单独的列表,如下所示,我假设第一个值是纬度,第二个是经度。
List<String> lats = new ArrayList<String>();
List<String> lons = new ArrayList<String>();
for (int i = 0; i < points.length ; i++)
{
if(i % 2 == 0)
{
lons.add(points[i]);
}
else
{
lats.add(points[i]);
}
}