Last active
July 6, 2019 09:26
-
-
Save pkavoo/357ac58be6615e793af4905c587d96a2 to your computer and use it in GitHub Desktop.
Java code for activity_main.xml layout file with fragment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.Manifest; | |
import android.content.pm.PackageManager; | |
import android.location.Location; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.os.Looper; | |
import android.view.View; | |
import android.widget.Toast; | |
import androidx.annotation.NonNull; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.core.app.ActivityCompat; | |
import com.example.siren.R; | |
import com.google.android.gms.common.ConnectionResult; | |
import com.google.android.gms.common.GoogleApiAvailability; | |
import com.google.android.gms.location.FusedLocationProviderClient; | |
import com.google.android.gms.location.LocationCallback; | |
import com.google.android.gms.location.LocationRequest; | |
import com.google.android.gms.location.LocationResult; | |
import com.google.android.gms.location.LocationServices; | |
import com.google.android.gms.maps.CameraUpdateFactory; | |
import com.google.android.gms.maps.GoogleMap; | |
import com.google.android.gms.maps.OnMapReadyCallback; | |
import com.google.android.gms.maps.SupportMapFragment; | |
import com.google.android.gms.maps.model.BitmapDescriptorFactory; | |
import com.google.android.gms.maps.model.CameraPosition; | |
import com.google.android.gms.maps.model.LatLng; | |
import com.google.android.gms.maps.model.Marker; | |
import com.google.android.gms.maps.model.MarkerOptions; | |
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback { | |
private static final int MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 5445; | |
private GoogleMap googleMap; | |
private FusedLocationProviderClient fusedLocationProviderClient; | |
private Marker currentLocationMarker; | |
private Location currentLocation; | |
private boolean firstTimeFlag = true; | |
private final View.OnClickListener clickListener = new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
if (view.getId() == R.id.map && googleMap != null && currentLocation != null) | |
MapsActivity.this.animateCamera(currentLocation); | |
} | |
}; | |
private final LocationCallback mLocationCallback = new LocationCallback() { | |
@Override | |
public void onLocationResult(LocationResult locationResult) { | |
super.onLocationResult(locationResult); | |
if (locationResult.getLastLocation() == null) | |
return; | |
currentLocation = locationResult.getLastLocation(); | |
if (firstTimeFlag && googleMap != null) { | |
animateCamera(currentLocation); | |
firstTimeFlag = false; | |
} | |
showMarker(currentLocation); | |
} | |
}; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_maps); | |
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); | |
supportMapFragment.getMapAsync(this); | |
findViewById(R.id.map).setOnClickListener(clickListener); | |
} | |
@Override | |
public void onMapReady(GoogleMap googleMap) { | |
this.googleMap = googleMap; | |
} | |
private void startCurrentLocationUpdates() { | |
LocationRequest locationRequest = LocationRequest.create(); | |
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); | |
locationRequest.setInterval(3000); | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(MapsActivity.this, | |
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, | |
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION); | |
return; | |
} | |
} | |
fusedLocationProviderClient.requestLocationUpdates(locationRequest, mLocationCallback, Looper.myLooper()); | |
} | |
private boolean isGooglePlayServicesAvailable() { | |
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance(); | |
int status = googleApiAvailability.isGooglePlayServicesAvailable(this); | |
if (ConnectionResult.SUCCESS == status) | |
return true; | |
else { | |
if (googleApiAvailability.isUserResolvableError(status)) | |
Toast.makeText(this, "Please Install google play services to use this application", Toast.LENGTH_LONG).show(); | |
} | |
return false; | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { | |
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | |
if (requestCode == MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION) { | |
if (grantResults[0] == PackageManager.PERMISSION_DENIED) | |
Toast.makeText(this, "Permission denied by uses", Toast.LENGTH_SHORT).show(); | |
else if (grantResults[0] == PackageManager.PERMISSION_GRANTED) | |
startCurrentLocationUpdates(); | |
} | |
} | |
private void animateCamera(@NonNull Location location) { | |
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); | |
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(getCameraPositionWithBearing(latLng))); | |
} | |
@NonNull | |
private CameraPosition getCameraPositionWithBearing(LatLng latLng) { | |
return new CameraPosition.Builder().target(latLng).zoom(16).build(); | |
} | |
private void showMarker(@NonNull Location currentLocation) { | |
LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()); | |
if (currentLocationMarker == null) | |
currentLocationMarker = googleMap.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.defaultMarker()).position(latLng)); | |
else | |
MarkerAnimation.animateMarkerToGB(currentLocationMarker, latLng, new LatLngInterpolator.Spherical()); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); | |
if (fusedLocationProviderClient != null) | |
fusedLocationProviderClient.removeLocationUpdates(mLocationCallback); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
if (isGooglePlayServicesAvailable()) { | |
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this); | |
startCurrentLocationUpdates(); | |
} | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
fusedLocationProviderClient = null; | |
googleMap = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment