Hello everyone,
I am new to android and I want to make an app that get my device (mobile phone) location and on my custom map it should show a marker (just like it shows my location when we open official google map) and after exiting/restarting the app it should save my last (visited) location marker on the map itself.
I have attached my code
What it does : it will get the location of the device(
Latitude and Longitude
) and put marker on the custom Google Map.
But
when it does not save my marker/location the map
Please Assist
What I have tried:
My Code so far in Android Studio
<pre>public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final String TAG = "MapActivity";
private GoogleMap mMap;
LocationManager locationManager;
DatabaseHelper myDB;
private FusedLocationProviderClient mFusedLocationProviderClient;
private Boolean mLocationPermissionGranted = false;
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
private static final String FINE_LOCATION = android.Manifest.permission.ACCESS_FINE_LOCATION;
private static final String COARSE_LOCATION = android.Manifest.permission.ACCESS_COARSE_LOCATION;
private static final float DEFAULT_ZOOM = 15f;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
getLocationPermission();
}
private void getLocationPermission()
{
Log.d(TAG, "getLocationPermission: getting location permission");
String[] permission = {android.Manifest.permission.ACCESS_FINE_LOCATION,
android.Manifest.permission.ACCESS_COARSE_LOCATION};
if (ContextCompat.checkSelfPermission(this.getApplicationContext(), FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this.getApplicationContext(), COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
initMap();
} else {
ActivityCompat.requestPermissions(this, permission, LOCATION_PERMISSION_REQUEST_CODE);
}
} else {
ActivityCompat.requestPermissions(this, permission, LOCATION_PERMISSION_REQUEST_CODE);
}
}
private void initMap() {
Log.d(TAG, "initMap: initilizing map");
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(MapActivity.this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
Toast.makeText(this, "Map is Ready", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onMapReady: Map is Ready");
mMap = googleMap;
if (mLocationPermissionGranted) {
getDeviceLocation();
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) {
return;
}
mMap.setMyLocationEnabled(true);
}
}
private void getDeviceLocation() {
Log.d(TAG, "getDeviceLocation: getting the device current location");
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
try {
if (mLocationPermissionGranted) {
Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()) {
Log.d(TAG, "onComplete: Found location");
Location currentLocation = (Location)task.getResult();
if(currentLocation !=null)
{
moveCamera(new LatLng(currentLocation.getLatitude(),currentLocation.getLongitude()),
DEFAULT_ZOOM,"Updated");
}
}
else
{
Log.d(TAG, "onComplete: Current location is null");
Toast.makeText(MapActivity.this, " unable to get current location", Toast.LENGTH_SHORT).show();
}
}
});
}
} catch (SecurityException e) {
Log.e(TAG, "getDeviceLocation: SecurityException" + e.getMessage());
}
}
private void moveCamera(LatLng latLng, float zoom, String title)
{
Log.d(TAG, "moveCamera: Moving camera to Latitude: "+latLng.latitude + ", Longitude :"+latLng.longitude);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
MarkerOptions options = new MarkerOptions().position(latLng).title(title);
mMap.addMarker(options);
HideSoftKeyboard();
}
private void HideSoftKeyboard()
{
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}