我知道我的问题很愚蠢,但我对Intents感到困惑。从下面的代码中我想将它分成两个类ShowMap和LocationMap。我怎么能这样做?拳头只包含地图(FragmentActivity),从那里我称之为第二类,我想在其中找到用户的当前位置并将标记返回到地图(第一个活动)。提前谢谢!这是我想要的课程分开:
public class LocationMap extends FragmentActivity implements LocationListener{
private GoogleMap googleMap;
private LatLng latLng;
TextView tvLocation;
MarkerOptions markerOptions;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_maps);
// 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 fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
// Getting GoogleMap object from the fragment
googleMap = fm.getMap();
//setting zoomIn-zoomOut bar in the screen
googleMap.getUiSettings().setZoomControlsEnabled(true);
// Enabling MyLocation Layer of Google Map
googleMap.setMyLocationEnabled(true);
//googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Getting Current Location
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null){
onLocationChanged(location);
new ReverseGeocodingTask(getBaseContext()).execute(latLng);
}
locationManager.requestLocationUpdates(provider, 20000, 0, this);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.location_maps, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
.getActionView();
searchView.setSearchableInfo(searchManager
.getSearchableInfo(getComponentName()));
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case R.id.action_search:
return true;
case R.id.menu_settings:
// refresh
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onLocationChanged(Location location) {
googleMap.clear();
// 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 = 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(10));
markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng);
// Executing ReverseGeocodingTask to get Address
new ReverseGeocodingTask(getBaseContext()).execute(latLng);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText( getApplicationContext(),
"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String>{
Context mContext;
public ReverseGeocodingTask(Context context){
super();
mContext = context;
}
// Finding address using reverse geocoding
@Override
protected String doInBackground(LatLng... params) {
Geocoder geocoder = new Geocoder(mContext);
double latitude = params[0].latitude;
double longitude = params[0].longitude;
List<Address> addresses = null;
String addressText="";
try {
addresses = geocoder.getFromLocation(latitude, longitude,1);
} catch (IOException e) {
e.printStackTrace();
}
if(addresses != null && addresses.size() > 0 ){
Address address = addresses.get(0);
addressText = String.format("%s, %s, %s",
address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
address.getLocality(),
address.getCountryName());
}
return addressText;
}
@Override
protected void onPostExecute(String addressText) {
TextView tvLocation = (TextView) findViewById(R.id.tv_location);
Log.v ("4",addressText );
tvLocation.setText(addressText);
// Setting the title for the marker.
markerOptions.title(addressText);
googleMap.addMarker(markerOptions);
}
}
}}