Android location permission error with null - java

I'm a student currently learning and try to play around with google map api. Right now, I'm facing a problem with the getting the location lat, long. Yesterday, I can use it just fine, but suddenly I cannot use it anymore today. The code is kinda long, because I wanted to show that the app asked for permission for the location to be turn on before using the apps but I don't think I get it correctly. I don't think the app starts like how I wanted it to start.
The process should be, from previous activity intent to this MapsActivity will prompt the request permission to open location, and once the location is on, the google map marker will update to the current location.
Right now, the apps just keeps on crashing.
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mMap;
FusedLocationProviderClient mFusedLocationClient;
LocationRequest mLocationRequest;
LocationCallback mLocationCallback;
double mLocationLat, mLocationLong;
LocationSettingsRequest.Builder mLocationSettingsBuilder;
SettingsClient client;
Task<LocationSettingsResponse> task;
private static final int REQUEST_CHECK_SETTINGS = 0x1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//toolbar.setTitle("AskForMech : Maps");
//toolbar.setLogo(R.drawable.ic_launcher);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(MapsActivity.this);
mLocationCallback = new LocationCallback(){
#Override
public void onLocationResult(LocationResult locationResult) {
if(locationResult==null){
return;
} else {
mLocationLat = locationResult.getLastLocation().getLatitude();
mLocationLong = locationResult.getLastLocation().getLongitude();
}
}
};
setLocationRequestSettings();
}
public void getLocation(){ //error is here
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mLocationLat = location.getLatitude();
mLocationLong = location.getLongitude();
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
getLocation(); //error is here
LatLng pos = new LatLng(mLocationLat, mLocationLong);
mMap.addMarker(new MarkerOptions().position(pos).title("You!"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(pos));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(pos, 18.00f));
}
#Override
protected void onResume() {
super.onResume();
//startLocationUpdate();
requestLocationUpdate();
}
#Override
protected void onPause() {
super.onPause();
if(mFusedLocationClient != null){
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
Toast.makeText(MapsActivity.this, "Listener is removed.", Toast.LENGTH_SHORT).show();
}
}
private void requestLocationUpdate(){
mLocationSettingsBuilder = new LocationSettingsRequest.Builder().addLocationRequest(mLocationRequest);
client = LocationServices.getSettingsClient(MapsActivity.this);
task = client.checkLocationSettings(mLocationSettingsBuilder.build());
task.addOnSuccessListener(MapsActivity.this, new OnSuccessListener<LocationSettingsResponse>() {
#Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
startLocationUpdate();
}
});
task.addOnFailureListener(MapsActivity.this, new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
if(e instanceof ResolvableApiException){
try{
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(MapsActivity.this, REQUEST_CHECK_SETTINGS);
}catch(IntentSender.SendIntentException sendEx) {
}
}
}
});
}
private void setLocationRequestSettings(){
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(3000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void startLocationUpdate(){
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(MapsActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MapsActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
showExplanation();
} else {
ActivityCompat.requestPermissions(MapsActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
0);
}
} else {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, null);
Toast.makeText(MapsActivity.this, "Location permission was granted!", Toast.LENGTH_SHORT).show();
}
}
private void showExplanation(){
AlertDialog.Builder builder = new AlertDialog.Builder(MapsActivity.this);
builder.setTitle("Requires Location Permission.");
builder.setMessage("This app needs location permission to get the location information.");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCompat.requestPermissions(MapsActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
0);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MapsActivity.this, "Sorry, this function cannot be used until permission is granted.",Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
}

you should use the callback method onLocationChanged() instead of getLastKnownLocation (). Indeed, getLastKnownLocation() can return null (see here)
I prefer to use onLocationChanged() that is called once a location change is detected. It turns out that it works great for me.
If you have any trouble to implement a LocationListener, take a look at this post : https://stackoverflow.com/a/42218626/3780625
Best

Related

FusedLocationProviderClient gives inaccurate location

I have made a simple sample of the FusedLocationProviderClient according to this: https://developer.android.com/training/location/request-updates#java
The issue is that I get a location every now and then (waaaay above an interval of 4000ms) and the accuracy is 2000 meters. I need a more precise location and more often. I get the feeling that the location client is not set up properly but at the same time, when using the locationmanager I also get a slow update rate on gps when using the gps provider, network provider also gives me an accuracy of 2000 meters.
Here is my code:
public class MainActivity extends Activity implements ActivityCompat.OnRequestPermissionsResultCallback {
Context context;
private LocationCallback locationCallback;
private FusedLocationProviderClient fusedLocationClient;
private LocationRequest locationRequest;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
locationRequest = new LocationRequest();
locationRequest.setFastestInterval(2000);
locationRequest.setInterval(4000);
locationRequest.setMaxWaitTime(5000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
fusedLocationClient = new FusedLocationProviderClient(this);
locationCallback = new LocationCallback() {
#Override
public void onLocationResult(#NonNull LocationResult locationResult) {
for (Location location : locationResult.getLocations()) {
Log.e("Got location from provider " + location.getProvider(), location.getLatitude() + ", " + location.getLongitude());
Log.e("getAccuracy", String.valueOf(location.getAccuracy()));
}
}
#Override
public void onLocationAvailability(#NonNull LocationAvailability locationAvailability) {
super.onLocationAvailability(locationAvailability);
Log.e("asdf", "available");
}
};
}
private void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
String[] PERMISSIONS = {android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_FINE_LOCATION};
ActivityCompat.requestPermissions(this, PERMISSIONS, 112);
return;
}
fusedLocationClient.requestLocationUpdates(locationRequest,
locationCallback,
Looper.getMainLooper());
}
private void stopLocationUpdates() {
fusedLocationClient.removeLocationUpdates(locationCallback);
}
#Override
public void onRequestPermissionsResult(int i, #NonNull String[] strings, #NonNull int[] ints) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
startLocationUpdates();
}
public void onPause() {
super.onPause();
stopLocationUpdates();
}
}
How can I resolve this?

Marker disappearing and title not showing

I am trying to add a marker on my Google maps by long-pressing on the location and displaying the name of the location, however, whenever I long-press the marker appears for a split second and then goes away (disappears).
I understand after the long-press the marker is supposed to stay and then show the title as well, but that is not working.
Please do let me know if anyone is facing any such issue and can help me with it.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener {
private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
centerMapOnLocation(lastKnownLocation, "Your Location");
}
}
}
public void centerMapOnLocation(Location location, String title) {
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.clear();
if (title != "Your Location"){
mMap.addMarker(new MarkerOptions().position(userLocation).title(title));
}
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 10));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setOnMapLongClickListener(this);
Intent intent = getIntent();
if (intent.getIntExtra("placeNumber", 0) == 0) {
//zoom in on the user's location
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
centerMapOnLocation(location, "Your Location");
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
};
if (Build.VERSION.SDK_INT < 23) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
centerMapOnLocation(lastKnownLocation, "Your Location");
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);
}
}
}
}
#Override
public void onMapLongClick(LatLng latLng) {
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
String address ="";
try {
List <Address> listAddresses= geocoder.getFromLocation(latLng.latitude,latLng.longitude,1);
if (listAddresses!=null&& listAddresses.size()>0){
if (listAddresses.get(0).getThoroughfare()!=null){
if (listAddresses.get(0).getSubThoroughfare()!=null){
address+=listAddresses.get(0).getSubThoroughfare()+" ";
}
address+= listAddresses.get(0).getThoroughfare();
}
}
} catch (IOException e) {
e.printStackTrace();
}
if (address == ""){
SimpleDateFormat sdf = new SimpleDateFormat("mm:HH yyyyMMdd", Locale.getDefault());
address = sdf.format(new Date());
}
mMap.addMarker(new MarkerOptions().position(latLng).title(address).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
}
You are clearing your map every time your location changes here:
#Override
public void onLocationChanged(Location location) {
centerMapOnLocation(location, "Your Location");
}
That's why your marker disappears right after you add it to the map. If you remove or comment out this line within your centerMapOnLocation method:
mMap.clear();
Then the markers no longer disappear.
Hope this helps!

Start Gps Tracking again when the android application On-Resumes()

I have build a system which is based upon tracking users current location dynamically as they move around places. The system works perfectly and updates current location of the users time after time into the firebase database. however when the application is minimized and resumed again to the same activity which is a map of course, it only shows the location of the user but does not update it in the fire-base database. now i know that the activity starts again from the OnResume() method, i have tried several things but every time i try to do something, it most of the time says that googleapiClient is not connected and then crashes right away. without manipulating the onResume, the app works perfectly but that does not update user position into the fire-base when the app is minimized and reopened from the onResume method. What should i do to the onResume() method that it starts sending user dynamic location into the firebase???
Below i have posted my code. Any help would be appreciated Thank You.
public class RequestLocationMap extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks
, GoogleApiClient.OnConnectionFailedListener
, LocationListener {
private static final String TAG = "RequestLocationMap";
private GoogleMap mMap;
private GoogleApiClient googleApiClient;
private LocationRequest locationRequest;
private Location lastLocation;
private Marker currentUserLocationMarker;
public static final int Request_User_Location_Code = 99;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_request_location_map);
Log.d(TAG, "onCreate: called");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkUserLocationPermission();
}
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady: called");
mMap = googleMap;
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
public boolean checkUserLocationPermission() {
Log.d(TAG, "checkUserLocationPermission: called");
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, Request_User_Location_Code);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, Request_User_Location_Code);
}
return false;
} else {
return true;
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
Log.d(TAG, "onRequestPermissionsResult: here");
switch (requestCode) {
case Request_User_Location_Code:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (googleApiClient == null) {
buildGoogleApiClient();
}
mMap.setMyLocationEnabled(true);
}
} else {
Toast.makeText(this, "Application requires location permission", Toast.LENGTH_SHORT).show();
}
}
}
protected synchronized void buildGoogleApiClient() {
Log.d(TAG, "buildGoogleApiClient: called");
googleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
googleApiClient.connect();
}
#Override
public void onConnected(#Nullable Bundle bundle) {
Log.d(TAG, "onConnected: called");
locationRequest = new LocationRequest();
locationRequest.setInterval(1000);
locationRequest.setFastestInterval(1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
PendingResult<LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());
result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
#Override
public void onResult(LocationSettingsResult result) {
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch (status.getStatusCode()) {
case LocationSettingsStatusCodes.SUCCESS:
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try {
status.startResolutionForResult(
RequestLocationMap.this, 1000);
} catch (IntentSender.SendIntentException e) {
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
break;
}
}
});
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, locationRequest, this);
}
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(#NonNull ConnectionResult connectionResult) {
}
#Override
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged: called");
lastLocation = location;
if (currentUserLocationMarker != null) {
currentUserLocationMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Location");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
currentUserLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(14f));
DatabaseReference myTracking = FirebaseDatabase.getInstance().getReference().child("MyTracking");
GeoFire geoFire = new GeoFire(myTracking);
geoFire.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(), new GeoLocation(location
.getLatitude(), location.getLongitude()));
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult: called");
switch (requestCode) {
case 1000:
switch (resultCode) {
case Activity.RESULT_OK: {
break;
}
case Activity.RESULT_CANCELED: {
// The user was asked to change settings, but chose not to
Toast.makeText(RequestLocationMap.this, "App required location permission", Toast.LENGTH_LONG).show();
finish();
break;
}
default: {
break;
}
}
break;
}
}
#Override
protected void onStop() {
Log.d(TAG, "onStop: called");
super.onStop();
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
DatabaseReference myTracking = FirebaseDatabase.getInstance().getReference().child("MyTracking");
GeoFire geoFire = new GeoFire(myTracking);
geoFire.removeLocation(FirebaseAuth.getInstance().getCurrentUser().getUid());
}
#Override
protected void onResume() {
Log.d(TAG, "onResume: called");
super.onResume();
}
}
The problem with your app is that in the lifecycle method onStop() you request to stop receiving location updates from the FusedLocation API, but in the reciprocal method of onResume(). you never request for location updates. Furthermore, with the way your code is structured even if you did register for location updates inside of onResume(), you'd run into a problem with the GoogleClient API because it may not (more than likely, isn't) connected by the time onResume() gets executed.
I recommend that you call the method buildGoogleApiClient() from onResume instead of onCreate() (still perform the permissions check). This way the GoogleClient API is always connected while the activity is resumed. Remember to call "disconnect" on the GoogleClient API inside of onPause(). Inside of the location listener callback method you should perform a null check on the GoogleMap reference to prevent any NP-exceptions. GoogleMaps may be null during the first few executions of onLocationChanged(Location) because it may take more time to initialize the MapFragment than it does to receive the first location update from the FusedLocation API. I've seen the location API deliver a location update milliseconds after I requested updates.
Also, just good practice you should pair lifecycle methods with their reciprocals:
onCreate() -> onDestroy()
onStart() -> onStop()
onResume -> onPause()
Mixing and matching lifecycle callbacks can bite you down the line when you expecting a happens-before relationship, such as registering and deregistering for location updates when the app is paused and resumed.

How to track friends location?

I am new in this field and I am trying to make application to track my friends like "snap-map". I am able to get my real-time location but I don't know what should I add to get real-time location of people who are using same application.
Here is my Java code, it will really helpful for me if you guys give me a code.
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {
private FirebaseAuth mAuth;
final static int PERMISSION_ALL = 1;
final static String[] PERMISSIONS = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
private GoogleMap mMap;
MarkerOptions mo;
Marker marker;
private Location user;
LocationManager locationManager;
private HashMap<Float, Location> otherUser = new HashMap<Float, Location>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mAuth = FirebaseAuth.getInstance();
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mo = new MarkerOptions().position(new LatLng(0, 0)).title("My Current Location");
if (Build.VERSION.SDK_INT >= 27 && !isPermissionGranted()) {
requestPermissions(PERMISSIONS, PERMISSION_ALL);
} else requestLocation();
if (!isLocationEnabled())
showAlert(1);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
marker = mMap.addMarker(mo);
}
//Called when the location has changed.
#Override
public void onLocationChanged(Location location) {
LatLng myCoordinates = new LatLng(location.getLatitude(), location.getLongitude());
marker.setPosition(myCoordinates);
mMap.moveCamera(CameraUpdateFactory.newLatLng(myCoordinates));
}
//Called when the provider status changes.
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
//Called when the provider is enabled by the user.
#Override
public void onProviderEnabled(String provider) {
}
//Called when the provider is disabled by the user. If requestLocationUpdates
#Override
public void onProviderDisabled(String provider) {
}
private void requestLocation() {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_HIGH);
String provider = locationManager.getBestProvider(criteria, true);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(provider, 10000, 10, this);
}
private boolean isLocationEnabled() {
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}
private boolean isPermissionGranted() {
if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Log.v("mylog","Permission is granted");
return true;
}else{
Log.v("mylog","Permission not granted");
return false;
}
}
private void showAlert(final int status) {
String message, title, btnText;
if (status == 1) {
message = "Your Locations Settings is set to 'Off'.\nPlease Enable Location to " +
"use this app";
title = "Enable Location";
btnText = "Location Settings";
} else {
message = "Please allow this app to access location!";
title = "Permission access";
btnText = "Grant";
}
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setCancelable(false);
dialog.setTitle(title)
.setMessage(message)
.setPositiveButton(btnText, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
if (status == 1) {
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(myIntent);
} else
requestPermissions(PERMISSIONS, PERMISSION_ALL);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface paramDialogInterface, int paramInt) {
finish();
}
});
dialog.show();
}

Current location shows on emulator but not on phone

I am setting up my map to track my location. Right at the start I want it to detect my Lat and Long and show where I am and update the marker location when I move.
When I test this on GenyMotion emulator and input my own positions, it works and shows updated location accordingly. But when I test it on a mobile device, I do not get any marker and get the "Location null" Toast message.
Since it is the first time on the device, there is probably no last known location. Thus wanting to make sure, I took the device and moved a distance (probably 50 meters or so back n forth) and still nothing updated on the device.
public class TrackMapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {
private GoogleMap mMap;
LocationManager locationManager;
Location location;
String provider;
Double lat, lng;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_track_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(), false);
try {
//Get last known user location
location = locationManager.getLastKnownLocation(provider);
if(location != null){
Toast.makeText(TrackMapsActivity.this, "Location NOT null", Toast.LENGTH_SHORT).show();
onLocationChanged(location);
}
else {
Toast.makeText(TrackMapsActivity.this, "Location null", Toast.LENGTH_SHORT).show();
}
}
catch (SecurityException e){
e.printStackTrace();
}
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
}
#Override
protected void onResume() {
super.onResume();
try {
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
catch (SecurityException e){
e.printStackTrace();
}
}
#Override
protected void onPause() {
super.onPause();
try {
locationManager.removeUpdates(this);
}
catch (SecurityException e){
e.printStackTrace();
}
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onLocationChanged(Location location) {
Toast.makeText(TrackMapsActivity.this, "You Moved", Toast.LENGTH_SHORT).show();
lat = location.getLatitude();
lng = location.getLongitude();
LatLng yourLocation = new LatLng(lat, lng);
if(mMap != null) {
mMap.clear();
mMap.addMarker(new MarkerOptions().position(yourLocation).title("Your Location"));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(yourLocation, 10));
}
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
do you put the permission for using the gps :)
The main permissions you need are
android.permission.ACCESS_COARSE_LOCATION or
android.permission.ACCESS_FINE_LOCATION.

Categories