2013-08-22 32 views
-1

我是新來的android編程,我嘗試在谷歌地圖v2 android上繪製折線。座標(超過100個)存儲在兩個txt文件(一個txt用於lat,一個用於lng),位於asstes目錄中。我嘗試加載字符串中的文件的內容,但現在我不知道如何將這些轉換爲雙摺線功能。 Double.parseDouble(contentdlat);將無法正常工作!如何從資產獲得一個字符串到latlng谷歌地圖v2 Android

在TXT的AR seperatet座標用 「」 看起來像:

dlat.txt = 42.4630,42.4539

dlng.txt = -75.0572,-73.9737

更新:現在我只使用一個文件而不是兩個。

coord_short.txt = 42.4630,-75.0572,42.4539,-73.9737

舊代碼如下所示:

//Add Polyline 
     ArrayList<LatLng> all=new ArrayList<LatLng>(); 
     ArrayList<Double> lat1=new ArrayList<Double>(); 
     ArrayList<Double> lon=new ArrayList<Double>(); 

     AssetManager assetManager = getAssets(); 

     // To load dlat text file 
     InputStream inputdlat; 
     try { 
     inputdlat = assetManager.open("dlat.txt"); 

     int sizedlat = inputdlat.available(); 
     byte[] bufferdlat = new byte[sizedlat]; 
     inputdlat.read(bufferdlat); 
     inputdlat.close(); 

     // byte buffer into a string 
     String contentdlat = new String(bufferdlat); 
     Toast.makeText(this, contentdlat, Toast.LENGTH_SHORT).show(); 
     //String[] splitdlat = contentdlat.split(","); 

     } 
     catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       } 


     // To load dlng text file 
     InputStream inputdlng; 
     try { 
     inputdlng = assetManager.open("dlng.txt"); 

     int sizedlng = inputdlng.available(); 
     byte[] bufferdlng = new byte[sizedlng]; 
     inputdlng.read(bufferdlng); 
     inputdlng.close(); 

     // byte buffer into a string 
     String contentdlng = new String(bufferdlng); 
     Toast.makeText(this, contentdlng, Toast.LENGTH_SHORT).show(); 
     //String[] splitdlng = contentdlng.split(","); 

     } 
     catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       } 

     double dlat = Double.parseDouble(contentdlat); 
     double dlat = Double.parseDouble(contentdlng); 

     //double[] dlat = {42.4630,42.4539}; 
     //double[] dlon = new double[]{-75.0572,-73.9737}; 

     for(double n : dlat){ 
     lat1.add(n); 
     } 

     for(double n : dlon){ 
     lon.add(n); 
     } 

     for(int a=0;a<lat1.size();a++) 
     { 
     LatLng allLatLng= new LatLng(lat1.get(a),lon.get(a)); 
     all.add(allLatLng); 
     } 

     Polyline polyline = map.addPolyline(new PolylineOptions() 
     .addAll(all) 
     .width(8) 
     .color(Color.GREEN)); 

這將是巨大的,如果有人能幫助我。

好與皮什·古普塔的幫助下,我改變了代碼:

  AssetManager assetManager = getAssets(); 

     // To load coordinate text with hundreds of coordinates file like 
     InputStream input; 
     try { 

     input = assetManager.open("coord_short.txt"); 

     int size = input.available(); 
     byte[] buffer = new byte[size]; 
     input.read(buffer); 
     input.close(); 

     // byte buffer into a string 
     String content = new String(buffer); 
     String[] separated = content.split(","); 
     String latString = separated[0]; 
     String longString = separated[1]; 

     double coordlat = Double.parseDouble(latString); 
     double coordlon = Double.parseDouble(longString); 
     LatLng coordlocation = new LatLng(coordlat, coordlon); 




      Polyline polyline = map.addPolyline(new PolylineOptions() 
      .add(coordlocation) 
      .width(8) 
      .color(Color.GREEN)); 



     } 
     catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       } 

但現在我的折線是不是在地圖上繪製。

什麼現在錯了?

+0

資產文件夾中是否有兩個不同的經度文件? – Piyush

+0

有一個緯度文件(dlat.txt)和一個帶有經度文件(dlng.txt)的文件。 – Schmidt

+0

但一般在這個dlat.txt = 42.4630,42.4539文件經緯度可用...所以 – Piyush

回答

0
InputStream is = getAssets().open("test.txt"); 
int size = is.available(); 
byte[] buffer = new byte[size]; //declare the size of the byte array with size of the file 
is.read(buffer); //read file 
is.close(); //close file 

// Store text file data in the string variable 
    String str_data = new String(buffer); 
+0

這段代碼看起來和我的代碼一樣,它們的區別在哪裏? – Schmidt