0

我有一个显示谷歌地图 v2 的活动。在那里,我有一些单选按钮,当我按下“保存”按钮时,我想保存我当前的位置。这个按钮在“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 );



    }



}

- - - - -更新 - - - - - - - - - - - - - -

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" 。该方法不适用于双参数。

4

2 回答 2

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();
        ...
}
于 2013-03-20T14:24:01.027 回答
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);
    }

这对我有用。

希望这对您有所帮助。

于 2013-03-20T15:03:30.467 回答