Created
May 1, 2019 11:40
-
-
Save p4yam/96736a1349860c296d05423bb7cf23c5 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 ir.kivee.testmap.view; | |
import android.Manifest; | |
import android.annotation.SuppressLint; | |
import android.content.Context; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.location.Location; | |
import android.location.LocationManager; | |
import android.os.Bundle; | |
import android.os.Looper; | |
import android.provider.Settings; | |
import android.support.annotation.NonNull; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v4.app.FragmentActivity; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.app.AlertDialog; | |
import android.util.Log; | |
import android.widget.Toast; | |
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.GoogleMap; | |
import com.google.android.gms.maps.OnMapReadyCallback; | |
import com.google.android.gms.maps.SupportMapFragment; | |
import com.google.android.gms.maps.model.LatLng; | |
import com.google.android.gms.maps.model.MarkerOptions; | |
import java.util.Calendar; | |
import java.util.List; | |
import ir.kivee.testmap.R; | |
import ir.kivee.testmap.data.MapDatabaseHelper; | |
import ir.kivee.testmap.model.UserMarker; | |
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { | |
private GoogleMap mMap; | |
private final static int LOCATION_REQUEST = 2008; | |
private LocationRequest mLocationRequest = null; | |
private FusedLocationProviderClient fusedLocationClient = null; | |
private Boolean mLocationPermissionGranted = false; | |
private LatLng currentLatLng = null; | |
private Float defaultZoom = 15f; | |
private MapDatabaseHelper db; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_maps); | |
db = new MapDatabaseHelper(this); | |
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() | |
.findFragmentById(R.id.map); | |
if (mapFragment != null) { | |
mapFragment.getMapAsync(this); | |
} | |
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) | |
handleLocationPermission(); | |
else { | |
mLocationPermissionGranted = true; | |
checkLocation(); | |
} | |
} | |
@Override | |
public void onMapReady(GoogleMap googleMap) { | |
mMap = googleMap; | |
findLocation(); | |
fetchData(); | |
} | |
private void handleLocationPermission() { | |
if (ContextCompat.checkSelfPermission( | |
this, | |
Manifest.permission.ACCESS_FINE_LOCATION | |
) != PackageManager.PERMISSION_GRANTED | |
) { | |
/*Ask if the app can use the permission*/ | |
RequestPermissionDialog(); | |
} else { | |
mLocationPermissionGranted = true; | |
checkLocation(); | |
} | |
} | |
private void RequestPermissionDialog() { | |
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this) | |
.setMessage(R.string.location_permission_request) | |
.setCancelable(false) | |
.setPositiveButton("Allow", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
/*Request location permission*/ | |
ActivityCompat.requestPermissions(MapsActivity.this, | |
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, | |
LOCATION_REQUEST); | |
} | |
}).setNegativeButton("DisAllow", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
Toast.makeText(MapsActivity.this, | |
R.string.location_permission_denied, Toast.LENGTH_LONG).show(); | |
dialog.dismiss(); | |
} | |
}); | |
AlertDialog dialog = alertDialog.create(); | |
if (!dialog.isShowing()) | |
dialog.show(); | |
} | |
private void enableGPSDialog() { | |
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this) | |
.setMessage("Please enable your GPS to start tracking.") | |
.setCancelable(false) | |
.setPositiveButton("OK", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); | |
} | |
}).setNegativeButton("Not Now", new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
Toast.makeText(MapsActivity.this, | |
R.string.location_permission_denied, Toast.LENGTH_LONG).show(); | |
dialog.dismiss(); | |
} | |
}); | |
AlertDialog dialog = alertDialog.create(); | |
if (!dialog.isShowing()) | |
dialog.show(); | |
} | |
private void checkLocation() { | |
LocationManager locationManager = (LocationManager) | |
getSystemService(Context.LOCATION_SERVICE); | |
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { | |
enableGPSDialog(); | |
} else | |
findLocation(); | |
} | |
@SuppressLint("MissingPermission") | |
private void findLocation() { | |
if (mMap != null && mLocationPermissionGranted) { | |
mLocationRequest = new LocationRequest(); | |
mLocationRequest.setInterval(5 * 1000); | |
mLocationRequest.setMaxWaitTime(2 * 60 * 1000); | |
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); | |
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this); | |
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this); | |
fusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper()); | |
mMap.setMyLocationEnabled(true); | |
} | |
} | |
LocationCallback mLocationCallback = new LocationCallback() { | |
@Override | |
public void onLocationResult(LocationResult locationResult) { | |
List<Location> locationList = locationResult.getLocations(); | |
Location location; | |
if (locationList.size() > 0) { | |
location = locationList.get(locationList.size() - 1); | |
String timeNow = Calendar.getInstance().getTime().toString(); | |
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); | |
MarkerOptions markerOptions = new MarkerOptions().position(latLng) | |
.title(timeNow); | |
mMap.addMarker(markerOptions); | |
saveData(timeNow, latLng); | |
} | |
} | |
}; | |
private void saveData(String timeNow, LatLng latLng) { | |
try { | |
db.addMarker(new UserMarker(timeNow, String.valueOf(latLng.latitude), | |
String.valueOf(latLng.longitude))); | |
} catch (Exception e) { | |
Toast.makeText(MapsActivity.this, "Error saving data" + e.getMessage(), | |
Toast.LENGTH_LONG).show(); | |
Log.d("maps", "onLocationResult: " + e.getMessage()); | |
} | |
} | |
private void fetchData() { | |
List<UserMarker> markerList = db.markerList(); | |
if (markerList.size() > 0 && mMap != null) | |
for (int i = 0; i < markerList.size(); i++) { | |
MarkerOptions markerOptions = new MarkerOptions().position( | |
markerList.get(i).getLocation()) | |
.title(markerList.get(i).getTime()); | |
mMap.addMarker(markerOptions); | |
} | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
findLocation(); | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions | |
, @NonNull int[] grantResults) { | |
Log.d("p4yam", "onRequestPermissionsResult: " + requestCode); | |
if (requestCode == LOCATION_REQUEST) { | |
if (grantResults.length > 0 | |
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
/*Location permission granted*/ | |
mLocationPermissionGranted = true; | |
checkLocation(); | |
} else { | |
Toast.makeText(MapsActivity.this, | |
R.string.location_permission_denied, Toast.LENGTH_LONG).show(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment