Latitude Longitude not appearing - android

I have the following function for retrieving latitude and longitude of the current location of the mobile.
private void getLocation() {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
double latti = location.getLatitude();
double longi = location.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);
textView.setText("Your current location is" + "\n" + "Lattitude = " + lattitude
+ "\n" + "Longitude = " + longitude);
} else {
Toast.makeText(this, "Unable to Trace your location", Toast.LENGTH_SHORT).show();
}
}
}
This function is being called through a button. The problem is that by clicking the button the function doesn't seem to find the location and keep on toasting the same message which is toasted in the else statement. Can someone please point out that what is it that i am doing wrong.

First you need to define a LocationListener
private final LocationListener mLocationListener = new LocationListener() {
#Override
public void onLocationChanged(final Location location) {
//your code here to get lat lon from location
}
And in the activity
mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME,
LOCATION_REFRESH_DISTANCE, mLocationListener);

Related

Get the CurrentLocation instead of LastKnownLocation

I made this code and it's working, but the location can only be retrieved if I open Google Maps before launching the app. I want to make my app detect my current location instead of the last known location. I found that there is a method called getCurrentLocation() that can be used to retrieve my location but I don't know what to put in the parameters. Please help me.
This is my code:
private void getLocation() {
//Check Permissions again
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(Reclamation.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Reclamation.this,
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.INTERNET}, REQUEST_PERMISSION_LOCATION);
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location LocationGps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location LocationNetwork = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location LocationPassive = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (LocationGps != null) {
double lat = LocationGps.getLatitude();
double longi = LocationGps.getLongitude();
latitude = String.valueOf(lat);
longitude = String.valueOf(longi);
} else if (LocationNetwork != null) {
double lat = LocationNetwork.getLatitude();
double longi = LocationNetwork.getLongitude();
latitude = String.valueOf(lat);
longitude = String.valueOf(longi);
} else if (LocationPassive != null) {
double lat = LocationPassive.getLatitude();
double longi = LocationPassive.getLongitude();
latitude = String.valueOf(lat);
longitude = String.valueOf(longi);
Toast.makeText(Reclamation.this, "Passive is not null :D", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(Reclamation.this, "Can't Get Your Location", Toast.LENGTH_SHORT).show();
}
Intent intent = new Intent(Reclamation.this, SetLocation.class);
intent.putExtra("latt", latitude);
intent.putExtra("lonn", longitude);
Toast.makeText(Reclamation.this, latitude + " " + longitude, Toast.LENGTH_SHORT).show();
startActivityForResult(intent, 3);
}
}
With
api 'com.google.android.gms:play-services-location:18.0.0'
U can use this methods
FusedLocationProviderClient fusedLocationClient;
FusedLocationProviderClient locationProviderClient = LocationServices.
getFusedLocationProviderClient(this);
//LOCATION AVAILABILITY
fusedLocationClient
.getLocationAvailability()
.addOnSuccessListener(new OnSuccessListener<LocationAvailability>() {
#Override
public void onSuccess(LocationAvailability locationAvailability) {
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});
//LAST LOCATION
fusedLocationClient.getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
#Override
public void onSuccess(Location location) {
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
}
});
//AND MORE...
fusedLocationClient
.requestLocationUpdates()
...

Not able to get current location in real devices

I need lat long of current location.I am getting lat long on emulator but not on real devices though my gps is on.
This is how I am getting current location.
public void getLocation() {
try {
if (ContextCompat.checkSelfPermission(listener, android.Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) ==
PackageManager.PERMISSION_GRANTED) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, this);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Logger.log("LastKnownLocation"+location);
gps_enabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
Logger.log("GPSEnabled"+gps_enabled);
} else {
ActivityCompat.requestPermissions(this, new String[] {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION },
1);
}
}
catch(SecurityException e) {
e.printStackTrace();
}
}
LocationListener listener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
Toast.makeText(this, "Current location"+ location.getLatitude() + ", " + location.getLongitude(), Toast.LENGTH_SHORT).show();
Logger.log("Current location"+ location.getLatitude() + ", " + location.getLongitude());
drawMarker(location);
}

Why Android cannot get location sometime

I use these code to get location
But sometime I cannot get the location inside building, and that time the WIFI is connected, Location is enabled.
How can I make sure getLastKnownLocation will return location when it is able to get location?
LocationManager locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null)
{
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if(location==null)
{
Toast.makeText(getApplicationContext(),"no location", Toast.LENGTH_SHORT).show();
return;
}
This works for me...
public void updateLoction(View view){
locationManger = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManger.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
txtLat.setText("" + location.getLatitude());
txtLon.setText("" + location.getLongitude());
}
locationManger.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
private void setLocation(Location location) {
if (location != null) {
txtLat.setText("" + location.getLatitude());
txtLon.setText("" + location.getLongitude());
}
locationManger.removeUpdates(this);
}

Turning on the GPS on android 4.4

I'm trying to get GPS Longitude & Latitude.
Everything is working in case that the GPS is turning on while using any other program that turning it on.
But when i trying to use only my app , the GPS is not turning on.
here is my code:
m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = m_locationManager.getBestProvider(m_c, false);
Location location = m_locationManager.getLastKnownLocation(provider);
if (location != null)
{
double lng = location.getLongitude();
double lat = location.getLatitude();
gpsLocationLon.setText("" + lng);
gpsLocationLat.setText("" + lat);
}
else
{
gpsLocationLon.setText("No Provider");
gpsLocationLat.setText("No Provider");
}
public void onLocationChanged(Location location) {
double lng = location.getLongitude();
double lat = location.getLatitude();
if(null != gpsLocationLat && null != gpsLocationLon)
{
gpsLocationLon.setText("" + lng);
gpsLocationLon.setText("" + lat);
}
}
what did i miss?
You need to start listening to location updates once you've found the best provider.
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider, 0, 0, this);
// ^^ This will start listening for location updates
// depending on your provider.
} else {
Log.d(LOGTAG, provider + " not enabled");
}
Remember that there are two types of providers, gps and network. So it depends on the criteria(m_c) which one is selected.
If you want to make sure that you want to listen to GPS as well as Network Updates, remove the Criteria variable and try this :
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
} else {
Log.d(LOGTAG, "network provider not enabled");
}
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
} else {
Log.d(LOGTAG, gps provider not enabled");
}
Edit 1:
m_locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (m_locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
m_locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
Location location = m_locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
else {
Log.d(LOGTAG, "network provider not enabled");
}
if (m_locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
m_locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else {
Log.d(LOGTAG, "gps provider not enabled");
}
if (location != null)
{
double lng = location.getLongitude();
double lat = location.getLatitude();
gpsLocationLon.setText("" + lng);
gpsLocationLat.setText("" + lat);
}
else
{
gpsLocationLon.setText("No Provider");
gpsLocationLat.setText("No Provider");
}
public void onLocationChanged(Location location) {
double lng = location.getLongitude();
double lat = location.getLatitude();
if(null != gpsLocationLat && null != gpsLocationLon)
{
gpsLocationLon.setText("" + lng);
gpsLocationLon.setText("" + lat);
}
}

getLastKnownLocation method always returns null

I have a problem to fixed position. I use getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
but always return null, i have set the permissions in your AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
and enabled in Settings --> Location and Security --> location through network.
TextView locationShow = (TextView) this.findViewById(R.id.location);
double latitude = 0.0, longitude = 0.0;
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
else {
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
if (location != null) {
Log.i("SuperMap", "Location changed : Lat: " + location.getLatitude() + " Lng: " +
location.getLongitude());
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0,
locationListener);
Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
locationShow.setText("经度:" + latitude + "纬度:" + longitude);
I find that other apps can show the location correctly, so maybe there something wrong with my code.
getLastKnownLocation() give the last valid cached location.
You are trying to get the cached location from the Network Provider. You have to wait for a few minutes till you get a valid fix. Since the Network Provider's cache is empty, you are obviously getting a null there.

Categories

Resources