0

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.

Peter Parker
  • 560
  • 1
  • 5
  • 17
  • A fully functioning SIM card might be needed for network based location to work. Some older discussion about that [in here](http://stackoverflow.com/questions/8755226/android-network-provider-sim-needed/13526071) and [in here](http://stackoverflow.com/questions/36018740/locationmanager-getlastknownlocationlocationmanager-network-provider-always-re/36035467). – Markus Kauppinen May 26 '16 at 11:18
  • @MarkusKauppinen I don't understand how can it help me? – Peter Parker May 26 '16 at 11:21
  • You have turned GPS off, there are probably no Wi-Fi base stations with a known location, the only way for the phone to determine the location is to communicate with the cell towers. Without a SIM card it might not be able or willing to communicate with the cell towers, hence no location. Looks like at least Samsung devices require a SIM as was concluded in the second link. And there are methods to check if SIM/network connection are needed [as mentioned in the documentation](https://developer.android.com/reference/android/location/LocationProvider.html?hl=en#requiresCell()). – Markus Kauppinen May 26 '16 at 11:34
  • @MarkusKauppinen so is there any way to get location with cell towers? – Peter Parker May 26 '16 at 13:28
  • Cell towers are one method used for the "network based" location. (The other one being Wi-Fi.) So yes, in theory the "Fused location provider" should try to use the cell towers. And the `LocationManager` as well if you specified the `NETWORK_PROVIDER`. So you have already attempted to get the location with cell towers. It just hasn't worked out for one reason or another. One thing is that `getLastLocation()` may well return null but you replied to akhil Rao that even `requestLocationUpdates()` didn't help. Can the Google Maps app pin-point your location? If not, the problem isn't in your code. – Markus Kauppinen May 26 '16 at 14:32
  • The `getLastLocation()` may well return null, because it does NOT initiate any actual action to determine the location. It just returns the location if it happens to be known. And it only "happens to be known" if some application has specifically requested a location update. Or possibly just connecting to the `GoogleApiClient` also triggers the location finding but the location isn't immediately available when you try to get the location in `onConnected()`. I'm not sure about this. Some of the older "getLastLocation() returns null" discussions might have some insight on that. – Markus Kauppinen May 26 '16 at 14:42
  • Did you get any solution? – user4057066 Aug 11 '17 at 13:20

1 Answers1

1

Try like this

First define LocationRequest object like this

   LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

Then inside onConnected() add this

  LocationServices.FusedLocationApi.requestLocationUpdates(
        mGoogleApiClient, mLocationRequest, this);

Then override the onLocationChanged() method

@Override
public void onLocationChanged(Location location) {
    //Here you can use location.getLatitude() etc
}
akhil Rao
  • 1,169
  • 7
  • 17