Android 谷歌地图在方向改变和事件处理中的应用

Android 谷歌地图在方向改变和事件处理中的应用,android,google-maps,gps,firebase-authentication,Android,Google Maps,Gps,Firebase Authentication,在这项活动中我有三个疑问 当用户在不打开GPS的情况下打开应用程序时,他们将看到默认位置 因此,如果我们可以在用户进入应用程序之前提醒他们使用GPS服务,或者如果他们在进入应用程序之后使用GPS服务,则位置会在地图上得到更新 当我试图保存位置和相机位置时,方向更改标记不可见 在onmapredy中获取用户的位置,但是如果用户移动,如何更新 public class HomeActivity extends AppCompatActivity implements OnMapRea

在这项活动中我有三个疑问

  • 当用户在不打开GPS的情况下打开应用程序时,他们将看到默认位置
  • 因此,如果我们可以在用户进入应用程序之前提醒他们使用GPS服务,或者如果他们在进入应用程序之后使用GPS服务,则位置会在地图上得到更新

  • 当我试图保存位置和相机位置时,方向更改标记不可见

  • 在onmapredy中获取用户的位置,但是如果用户移动,如何更新

    public class HomeActivity extends AppCompatActivity 
           implements OnMapReadyCallback, 
            GoogleApiClient.OnConnectionFailedListener,
            GoogleApiClient.ConnectionCallbacks,
            LocationListener{
    
    @BindView(id.toolbar)
    Toolbar toolbar;
    @BindView(id.recycler_list)
    RecyclerView recyclerList;
    
    private GoogleMap mMap;
    private MarkerOptions marker;
    private boolean mLocationPermissionGranted;
    private int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
    private Location mLastKnownLocation;
    private CameraPosition mCameraPosition;
    private float DEFAULT_ZOOM = 15;
    private static final String KEY_CAMERA_POSITION = "camera_position";
    private static final String KEY_LOCATION = "location";
    
    private FirebaseAuth mFirebaseAuth;
    private FirebaseUser mFirebaseUser;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private  GoogleApiClient googleApiClient;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(layout.activity_home);
        ButterKnife.bind(this);
    
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    
        mFirebaseAuth = FirebaseAuth.getInstance();
        mFirebaseUser = mFirebaseAuth.getCurrentUser();
    
        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(FirebaseAuth firebaseAuth) {
                if (mFirebaseUser == null) {
                    startActivity(new Intent(getApplicationContext(), SignActivity.class));
                    finish();
                    return;
                }
            }
        };
    
        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();
        googleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this ,this)
                .addApi(Auth.GOOGLE_SIGN_IN_API , gso)
                .addConnectionCallbacks(this)
                .addApi(LocationServices.API)
                .addOnConnectionFailedListener(this)
                .build();
    
        if (savedInstanceState != null) {
            mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
            mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
        }
    }
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
    
        updateLocationUI();
        getDeviceLocation();
    }
    
    private void updateLocationUI() {
        if (mMap == null) {
            return;
        }
    
        if (ContextCompat.checkSelfPermission(this.getApplicationContext(),
                android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mLocationPermissionGranted = true;
        } else {
            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
        }
    
        if (mLocationPermissionGranted) {
            mMap.setMyLocationEnabled(true);
            mMap.getUiSettings().setMyLocationButtonEnabled(true);
        } else {
            mMap.setMyLocationEnabled(false);
            mMap.getUiSettings().setMyLocationButtonEnabled(false);
            mLastKnownLocation = null;
        }
    }
    
    private void getDeviceLocation() {
    
        if (mLocationPermissionGranted) {
            mLastKnownLocation = LocationServices.FusedLocationApi
                    .getLastLocation(googleApiClient);
        }
    
        if (mCameraPosition != null) {
            mMap.moveCamera(CameraUpdateFactory.newCameraPosition(mCameraPosition));
        } else if (mLastKnownLocation != null) {
            LatLng location = new LatLng(mLastKnownLocation.getLatitude(),
                    mLastKnownLocation.getLongitude());
            mMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE))
                    .position(location)).setTitle(mFirebaseUser.getDisplayName());
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location,DEFAULT_ZOOM));
    
        } else {
            LatLng mDefaultLocation = new LatLng(-31,40);
            mMap.addMarker(new MarkerOptions().position(mDefaultLocation)).setTitle("Unknown");
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM));
            mMap.getUiSettings().setMyLocationButtonEnabled(false);
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
    
        if (id == R.id.action_profile) {
            Intent intent = new Intent(HomeActivity.this, ProfileActivity.class);
            intent.putExtra("name", mFirebaseUser.getDisplayName());
            intent.putExtra("email", mFirebaseUser.getEmail());
            intent.putExtra("photoUrl",mFirebaseUser.getPhotoUrl().toString());
            startActivity(intent);
        }else if (id == R.id.action_event) {
            Intent intent = new Intent(HomeActivity.this, CreateEventActivity.class);
            startActivity(intent);
        }else if (id == R.id.action_logout) {
            mFirebaseAuth.getInstance().signOut();
            startActivity(new Intent(HomeActivity.this, SignActivity.class));
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
    
    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Toast.makeText(this, "Connection Failed" , Toast.LENGTH_LONG).show();
    }
    @Override
    public void onStart() {
        super.onStart();
        mFirebaseAuth.addAuthStateListener(mAuthListener);
        googleApiClient.connect();
    }
    @Override
    public void onStop() {
        googleApiClient.disconnect();
        if (mAuthListener != null) {
            mFirebaseAuth.removeAuthStateListener(mAuthListener);
        }
        super.onStop();
    }
    
    @Override
    public void onConnected(@Nullable Bundle bundle) {
        SupportMapFragment mapFragment = (SupportMapFragment) this.getSupportFragmentManager()
                .findFragmentById(id.map);
        mapFragment.getMapAsync(this);
    }
    
    @Override
    public void onConnectionSuspended(int i) {
    
    }
    
    @Override
    public void onLocationChanged(Location location) {
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    
    }
    
    @Override
    public void onProviderEnabled(String provider) {
    
    }
    
    @Override
    public void onProviderDisabled(String provider) {
    
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        if (mMap != null) {
            outState.putParcelable(KEY_CAMERA_POSITION, mMap.getCameraPosition());
            outState.putParcelable(KEY_LOCATION, mLastKnownLocation);
            super.onSaveInstanceState(outState);
        }
    }
    
    }


  • 1.对于方向更改问题,您必须在AndroidManifest.xml中进行如下更改:

    <activity
                android:name=".activity.VerificationActivity"
                android:configChanges="orientation|screenSize"/>
    
    AlertDialogManager.java:

    public class AlertDialogManager {
        public static void showInternetEnableAlertToUser(final Context context) {
            AlertDialog.Builder altForInternet = new AlertDialog.Builder(context);
    
            // User can't cancel dialog
            altForInternet.setCancelable(false);
    
            // Setting Dialog Title
            altForInternet.setTitle("Internet Enable Or Not ?");
    
            // Setting Dialog Message
            altForInternet.setMessage("Internet Is Not Enable. \nDo You Want To Enable ?");
    
            // On pressing Settings button
            altForInternet.setPositiveButton("YES", new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(
                            Settings.ACTION_WIFI_SETTINGS);
                    context.startActivity(intent);
                }
    
            });
    
            // Showing Alert Message
            altForInternet.show();
    
        }
    
        public static void showInternetEnableAlertToUser1(final Context context) {
            AlertDialog.Builder altForInternet = new AlertDialog.Builder(context);
    
            altForInternet.setCancelable(false);
            // Setting Dialog Title
            altForInternet.setTitle("PLease Connect With Internet ");
    
            // Setting Dialog Message
            altForInternet.setMessage("Internet Connection Is Not Available. \nPlease Connect With Net And Restart Application");
    
            // On pressing Settings button
            altForInternet.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    System.exit(0);
                }
    
            });
    
            // Showing Alert Message
            altForInternet.show();
    
        }
    
        public static void showGPSDisabledAlertToUser(final Context context) {
            // TODO Auto-generated method stub
            AlertDialog.Builder altForGPS = new AlertDialog.Builder(context);
    
            altForGPS.setTitle("GPS Enable Or Not ?");
    
            // Setting Dialog Message
            altForGPS.setMessage("GPS Is Not Enable. \nDo You Want To Enable ?");
            altForGPS.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Intent callGPSSettingIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            context.startActivity(callGPSSettingIntent);
                        }
                    });
            altForGPS.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = altForGPS.create();
            alert.show();
        }
    }
    

    我希望这可能会帮助您..

    关注此链接:->对于我们来说,关注此链接:->/在帖子中,这就像工作已经完成一样,但由于我正在使用GoogleapClient并遵循此链接,如果您可以帮助完成上述代码。
    public class AlertDialogManager {
        public static void showInternetEnableAlertToUser(final Context context) {
            AlertDialog.Builder altForInternet = new AlertDialog.Builder(context);
    
            // User can't cancel dialog
            altForInternet.setCancelable(false);
    
            // Setting Dialog Title
            altForInternet.setTitle("Internet Enable Or Not ?");
    
            // Setting Dialog Message
            altForInternet.setMessage("Internet Is Not Enable. \nDo You Want To Enable ?");
    
            // On pressing Settings button
            altForInternet.setPositiveButton("YES", new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    Intent intent = new Intent(
                            Settings.ACTION_WIFI_SETTINGS);
                    context.startActivity(intent);
                }
    
            });
    
            // Showing Alert Message
            altForInternet.show();
    
        }
    
        public static void showInternetEnableAlertToUser1(final Context context) {
            AlertDialog.Builder altForInternet = new AlertDialog.Builder(context);
    
            altForInternet.setCancelable(false);
            // Setting Dialog Title
            altForInternet.setTitle("PLease Connect With Internet ");
    
            // Setting Dialog Message
            altForInternet.setMessage("Internet Connection Is Not Available. \nPlease Connect With Net And Restart Application");
    
            // On pressing Settings button
            altForInternet.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    System.exit(0);
                }
    
            });
    
            // Showing Alert Message
            altForInternet.show();
    
        }
    
        public static void showGPSDisabledAlertToUser(final Context context) {
            // TODO Auto-generated method stub
            AlertDialog.Builder altForGPS = new AlertDialog.Builder(context);
    
            altForGPS.setTitle("GPS Enable Or Not ?");
    
            // Setting Dialog Message
            altForGPS.setMessage("GPS Is Not Enable. \nDo You Want To Enable ?");
            altForGPS.setPositiveButton("Yes",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Intent callGPSSettingIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            context.startActivity(callGPSSettingIntent);
                        }
                    });
            altForGPS.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = altForGPS.create();
            alert.show();
        }
    }