I try to get coordinates of user using internet(but without GPS)
Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
in onCreate()
method I initiate client
:
if (client == null) {
client = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
According to `developer.android.com`'s docs:
@Override
protected void onStart() {
client.connect();
super.onStart();
}
@Override
protected void onStop() {
client.disconnect();
super.onStop();
}
And onConnected
:
@Override
public void onConnected(@Nullable Bundle bundle) {
System.out.println("connected");
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;
}
location = LocationServices.FusedLocationApi.getLastLocation(client);
if (location != null) {
System.out.println("location " + location.getLatitude());
} else {
System.out.println("location " + "empty");
}
}
So, when my GPS is turned on, I can see coordinates in logs, but when I try to get it by the Internet(3g or Wi-Fi doesn't matter) my Location
is null
.
What did I miss? Maybe I'm on a wrong way?
P.S. Trying to make the same by using LocationManager
gave the same result.