我正在我的安卓应用程序中使用地图。当我导航到其他活动并返回主页(即地图活动(时,我将 MapsActivity 作为我的启动器活动,然后相机会动画到当前位置。我想要moveCamera而不是animateCamera。所以,我想有条件地将animateCamera方法更改为moveCamera,就像当应用程序启动时它应该使用animateCamera,之后,它将使用moveCamera。
这是完成此任务的正确方法吗?如果没有,请告诉我在我的应用程序中实现此功能的最佳方法
这是我的地图活动.java
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
// toolbar instance
private android.support.v7.widget.Toolbar mToolbar;
RelativeLayout rootLayout;
private DrawerLayout mDrawerlayout;
private ActionBarDrawerToggle mToggle;
private NavigationView mNavigationView;
//Request code
private static final int PERMISSION_REQUEST_CODE = 1998;
private static final int PLAY_SERVICES_REQUEST_CODE = 1999;
//Google APi client
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
private Location mLastLocation;
Marker mCurrentMarker;
private GoogleMap mMap;
double lattitude, longitude;
//Map intervals
private static int UPDATE_INTERVAL = 5000;
private static int FASTEST_INTERVAL = 3000;
private static int DISPLACEMENT = 10;
//Firebase
DatabaseReference mUserDatabase, locationDatabaseRef;
private FirebaseAuth mAuth;
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
//Custom Toolbar
mToolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("No Title");
mDrawerlayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerlayout, R.string.open, R.string.close);
mDrawerlayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mNavigationView = (NavigationView) findViewById(R.id.navigation_home);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
if (menuItem.getItemId() == R.id.navigationAccount) {
Intent profileIntent = new Intent(MapsActivity.this, ProfileActivity.class);
startActivity(profileIntent);
}
if (menuItem.getItemId() == R.id.navigationSettings) {
Intent settingsIntent = new Intent(MapsActivity.this, SettingsActivity.class);
startActivity(settingsIntent);
}
if (menuItem.getItemId() == R.id.navigationContact) {
}
return false;
}
});
mAuth = FirebaseAuth.getInstance();
//set contentView
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/Arkhip_font.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
// Firebase initialization
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
locationDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Locations");
setUpLocation();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (checkPlayServices()) {
buildGoogleApiClient();
createLocationRequest();
displayLocation();
}
}
break;
}
}
private void setUpLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestRuntimePermission();
} else {
if (checkPlayServices()) {
buildGoogleApiClient();
createLocationRequest();
displayLocation();
}
}
}
private void displayLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
lattitude = mLastLocation.getLatitude();
longitude = mLastLocation.getLongitude();
//Updating to firebase
locationDatabaseRef.child(mAuth.getCurrentUser().getUid())
.setValue(new Tracking(mAuth.getCurrentUser().getEmail(),
mAuth.getCurrentUser().getUid(),
String.valueOf(mLastLocation.getLatitude()),
String.valueOf(mLastLocation.getLongitude())));
mCurrentMarker = mMap.addMarker(new MarkerOptions()
.position(new LatLng(lattitude, longitude))
.title("You are Here"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lattitude , longitude) , 12.02f));
// mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lattitude, longitude), 12.02f));
}
}
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
private boolean checkPlayServices() {
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this, PLAY_SERVICES_REQUEST_CODE).show();
} else {
Toast.makeText(this, "This device is not supported", Toast.LENGTH_SHORT).show();
finish();
}
return false;
}
return true;
}
private void requestRuntimePermission() {
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
}, PERMISSION_REQUEST_CODE);
}
private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.getUiSettings().setMapToolbarEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
displayLocation();
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
displayLocation();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (item.getItemId() == R.id.connect_item) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Make Connection");
dialog.setMessage("Please use ID shown in profile");
LayoutInflater inflater = LayoutInflater.from(this);
final View connect_layout = inflater.inflate(R.layout.connect_layout, null);
final MaterialEditText connectID = connect_layout.findViewById(R.id.connect_id);
dialog.setView(connect_layout);
dialog.setPositiveButton("Connect", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//validation
if (TextUtils.isEmpty(connectID.getText().toString())) {
Snackbar.make(rootLayout, "Please enter ID", Snackbar.LENGTH_SHORT)
.show();
return;
}
mUserDatabase.orderByChild("uniqueID").equalTo(connectID.getText().toString())
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
// giving connected user data Log.d("QueryLog",dataSnapshot.getValue().toString());
// Getting lattitude and longitude
Toast.makeText(MapsActivity.this, "Yo ! Connected Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MapsActivity.this, "User Not Exist", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
// this is cancel btn for dialog
dialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
}
// Logout is here
if (item.getItemId() == R.id.logout) {
FirebaseAuth.getInstance().signOut();
Intent MainIntent = new Intent(MapsActivity.this, StartActivity.class);
startActivity(MainIntent);
finish();
}
// Profile item is here
if (item.getItemId() == R.id.profile_item) {
Intent profileIntent = new Intent(MapsActivity.this, ProfileActivity.class);
startActivity(profileIntent);
}
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return true;
}
}
这是我的地图活动.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/appBarLayout">
<include layout="@layout/main_app_bar_layout"
android:id="@+id/main_page_toolbar" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerLayout"
android:layout_marginTop="@dimen/cast_libraries_material_featurehighlight_inner_radius">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_below="@id/main_page_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity"
tools:ignore="NotSibling" />
<android.support.design.widget.NavigationView
android:id="@+id/navigation_home"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_header_layout">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
在你的地图中做这样的事情活动
//declare a global boolean
boolean hasAnimated = false;
现在在displayLocation()
if(!hasAnimated){
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lattitude , longitude) , 12.02f));
hasAnimated = true;
}else{
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lattitude, longitude), 12.02f));
}
这就行了。 :)