我有一個活動,它顯示谷歌地圖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。
你解決了嗎? – AwadKab 2013-03-20 14:45:51
@AwadKab:我正在嘗試。我更新了。它給我一個錯誤bw.write.Thanks – George 2013-03-20 14:55:25