Created
October 15, 2019 15:01
-
-
Save bariskarapinar/42ef1f03acf20fcd3909b5e838edca43 to your computer and use it in GitHub Desktop.
This file contains 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
package com.example.location_maps; | |
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.support.annotation.NonNull; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.widget.Toast; | |
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 MapActivity 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.currentLocationImageButton && googleMap != null && currentLocation != null) | |
MapActivity.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_map); | |
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment); | |
supportMapFragment.getMapAsync(this); | |
findViewById(R.id.currentLocationImageButton).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, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(MapActivity.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, "Install google play services", 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", 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