2013-03-20 48 views
0

我有一個活動,它顯示谷歌地圖V2。 在那裏,我有一些單選按鈕,當我按下「保存」按鈕時,我想保存當前位置。此按鈕位於「onMyLocationChange」函數中。從「onMyLocationChange」功能保存GPS位置onMyLocationChange功能

的問題是,緯度和經度都在裏面 「onMyLocationChange」 功能..

活動是:

public class selection extends FragmentActivity implements OnMyLocationChangeListener{ 

    GoogleMap googleMap; 


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

     // Getting Google Play availability status 
     int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext()); 

     // Showing status 
     if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available 
      int requestCode = 10; 
      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode); 
      dialog.show(); 

     }else { // Google Play Services are available 

      // Getting reference to the SupportMapFragment of activity_main.xml 
      SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 

      // Getting GoogleMap object from the fragment 
      googleMap = mapFragment.getMap(); 
      //set map type 
      googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 

      // Enabling MyLocation Layer of Google Map 
      googleMap.setMyLocationEnabled(true); 

      // Setting event handler for location change 
      googleMap.setOnMyLocationChangeListener(this); 


     RadioGroup rgViews = (RadioGroup) findViewById(R.id.rg_views); 

     rgViews.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       if(checkedId == R.id.rb_normal){ 
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
       }else if(checkedId == R.id.rb_satellite){ 
        googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
       }else if(checkedId == R.id.rb_terrain){ 
        googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN); 
       } 
       else if(checkedId == R.id.save_location){ 

        //here I want to save the location 
       } 
      } 
     }); 
     } 
    } 



    @Override 
    public void onMyLocationChange(Location location) { 
     TextView tvLocation = (TextView) findViewById(R.id.tv_location); 

     // Getting latitude of the current location 
     double latitude = location.getLatitude(); 

     // Getting longitude of the current location 
     double longitude = location.getLongitude(); 

     // Creating a LatLng object for the current location 
     LatLng latLng = new LatLng(latitude, longitude); 

     // Showing the current location in Google Map 
     googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 

     // Zoom in the Google Map 
     googleMap.animateCamera(CameraUpdateFactory.zoomTo(15)); 

     // Setting latitude and longitude in the TextView tv_location 
     tvLocation.setText("Latitude:" + latitude + ", Longitude:"+ longitude); 



    } 



} 

--------- UPDATE ----- ----------------------

else if(checkedId == R.id.save_location){ 

        FileOutputStream fos; 

        File filePath = new File(Environment.getExternalStorageDirectory() + "/What_Where.txt"); 

        try { 
         fos = new FileOutputStream(filePath); 
         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos)); 
         bw.write(latitude); 
         bw.write(longitude); 
         fos.flush(); 
         fos.close(); 
        } catch (FileNotFoundException e) { 
         Log.e("Saving", e.getMessage(), e); 
        } catch (IOException e) { 
         Log.e("Saving", e.getMessage(), e); 
        } 

它給我在「bw.write」。該方法不適用於參數double。

+0

你解決了嗎? – AwadKab 2013-03-20 14:45:51

+0

@AwadKab:我正在嘗試。我更新了。它給我一個錯誤bw.write.Thanks – George 2013-03-20 14:55:25

回答

0

試試這個

bw.write(String.valueOf(latitude)); 
bw.write(String.valueOf(longitude)); 

編輯

使用此寫入文件

File filePath = new File(Environment.getExternalStorageDirectory() +"/What_Where.txt"); 
    try { 
     BufferedWriter bw = new BufferedWriter(new FileWriter(filePath, true)); 
     bw.append(String.valueOf(latitude)); 
     bw.append(String.valueOf(longitude)); 
     bw.close(); 
    } catch (FileNotFoundException e) { 
     Log.e("Saving", e.getMessage(), e); 
    } catch (IOException e) { 
     Log.e("Saving", e.getMessage(), e); 
    } 

這爲我工作。

希望這對你有所幫助。

+0

:這個工程,但我保存的文件是empty.Am我wronf在保存?或者它與聲明字符串變量作爲全球性,當我問他們,他們沒有從location.getlongitude()的值... – George 2013-03-20 15:19:45

+0

:我認爲問題是,在「onCheckedChanged」我使用全局變量是空的。我需要一種方法來使用「onMyLocationChange」中的變量。或者我需要能夠從「else if(checkedId == R.id.save_location){」,但是可以調用「onMyLocationChange」我想不出來.. – George 2013-03-20 15:43:47

+0

你可以用'Log.d(「lat」,「」+ latitude);'來檢查。把它放在'else if(checkedId == R.id.save_location)'中,然後檢查LogCat。 – AwadKab 2013-03-20 15:56:54

1
GoogleMap googleMap; 
    double latitude=0.0; <--- Declare as Global variable 
    double longitude=0.0; <--- Declare as Global variable 

public void onMyLocationChange(Location location) { 

     latitude = location.getLatitude(); 
     longitude = location.getLongitude(); 
     ... 
} 
+0

:「onCheckedChanged」內的這些變量函數將具有什麼樣的價值?他們會提取「onMyLocationChange」的值嗎? – George 2013-03-20 14:37:07

+0

看到我的編輯答案 – MAC 2013-03-20 15:59:06

+0

:仍然收到一個空文件.. – George 2013-03-20 16:05:01