1

我将一些值存储在一个文本文件中,每一行看起来像这样,Cellid: -----, LAC: ------, Lat: ------, Long: ----- ----我想要做的是将手机当前运行的cellid与文本文件中的cellid进行比较,如果匹配,则在谷歌地图中打印一个标记。我的问题是如何在阅读文本文件时导航。该文件存储在手机的 SD 卡中。

4

2 回答 2

0
    public static final String Key_Cellid = "Cellid";
    public static final String Key_LAC = "LAC";
    public static final String Key_Lat = "Lat";
    public static final String Key_Long = "Long";
    ArrayList<HashMap<String,String>> values = new ArrayList<HashMap<String, String>>();
    int cellidtt,lact;
    double latt,longt;

    public void stringExtracter(String st){

         String mainStr = st;

            int items = 4; // Numbers of items in each line

            HashMap<String,String> hash = new HashMap<String, String>();

            for(int i=0; i<items; i++){
                String num="";
                int sep = mainStr.indexOf(":");
                String fst = mainStr.substring(0, sep);
                mainStr = mainStr.replaceFirst(fst+":", "");
                if(i<items - 1){
                    sep = mainStr.indexOf(",");
                    num = mainStr.substring(0, sep);
                }
                else{
                    num = mainStr;
                    mainStr = mainStr.replaceFirst(num+",", "");
                }

                hash.put(fst, num);
            }
             values.add(hash);
        }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        GoogleMap gmap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapg)).getMap();



        // Show the Up button in the action bar.
       // getActionBar().setDisplayHomeAsUpEnabled(true);
        GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

        gmap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        gmap.setMyLocationEnabled(true);

      File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
      File file = new File(sdcard,"cellidt.txt");

      try {
          BufferedReader br = new BufferedReader(new FileReader(file));
          String line;

          while ((line = br.readLine()) != null) {
             Log.d("line=",line); // print the text file in the LogCat to ensure that the program is reading the file proper.
             stringExtracter(line);
          }

         //here you can get only numbers from text and convert it to integer then compare it
          for(int i=0;i<values.size();i++){

              HashMap<String,String> hash = values.get(i);
              cellidtt = Integer.parseInt(hash.get(Key_Cellid));
              Log.d("cellidtt=",hash.get(Key_Cellid));
              lact = Integer.parseInt(hash.get(Key_LAC));
              Log.d("lact=",hash.get(Key_LAC));
              latt = Double.parseDouble(hash.get(Key_Lat));
              Log.d("latt=",hash.get(Key_Lat));
              longt = Double.parseDouble(hash.get(Key_Long));
              Log.d("longt=",hash.get(Key_Long));

              gmap.addMarker(new MarkerOptions()
                    .position(new LatLng(latt,longt))
                    .title(cellidtt+","+lact));

          }
      br.close();
      }
      catch (IOException e) {

      }

    }
于 2013-03-10T17:49:04.320 回答
0

尝试这个

第一:在公共区域添加这些

public static final String Key_Cellid = "Cellid";
public static final String Key_LAC = "LAC";
public static final String Key_Lat = "Lat";
public static final String Key_Long = "Long";
ArrayList<HashMap<String,String>> values = new ArrayList<HashMap<String, String>>();

然后添加此方法以分隔每个项目的名称和值:

public void stringExtracter(String st){

 String mainStr = st;

    int items = 4; // Numbers of items in each line

    HashMap<String,String> hash = new HashMap<String, String>();

    for(int i=0; i<items; i++){
        String num="";
        int sep = mainStr.indexOf(":");
        String fst = mainStr.substring(0, sep);
        mainStr = mainStr.replaceFirst(fst+":", "");
        if(i<items - 1){
            sep = mainStr.indexOf(",");
            num = mainStr.substring(0, sep);
            mainStr = mainStr.replaceFirst(num+",", "");
        }
        else{
            num = mainStr;
            //mainStr = mainStr.replaceFirst(num+",", "");
        }

        hash.put(fst, num);
    }
     values.add(hash);
}

现在编辑您的代码:

   //Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {

       stringExtracter(line);
    }

   //here you can get only numbers from text and convert it to integer then compare it
}
catch (IOException e) {
    //You'll need to add proper error handling here
}

知道您可以从values数组列表中获取数据,例如:

for(int i=0;i<values.size();i++){
        HashMap<String,String> hash = values.get(i);
        System.out.println(hash.get(Key_Cellid));
        System.out.println(hash.get(Key_LAC));
        System.out.println(hash.get(Key_Lat));
        System.out.println(hash.get(Key_Long));
        System.out.println("--------------");
    }

重要的提示:

我假设文本文件的每一行都没有空格

Cellid:123,LAC:456,Lat:223,Long:213

我将数字存储为字符串,因为您应该将其转换为 Integer 或 Double 或您想在 map 中使用它。

希望这对您有所帮助。

于 2013-03-09T00:23:47.580 回答