Get current location name of user without using gps or internet but by using Network_Provider in android

前端 未结 6 597
南方客
南方客 2020-12-31 08:50

This question is directly related to the same prevailing stackoverflow question at \"Android: get current location of user without using gps or internet\" where the accepted

6条回答
  •  有刺的猬
    2020-12-31 09:27

    The problem is that the code you tried does work, probably just not as well as you wished. For example, the accuracy such a method provides on Samsung Galaxy S3 is 2000m, meaning the actual position is anywhere within a circle of 2 kilometers radius. Additional it would probably take quite a large change in location before your app would be informed of a location change since the margin of error is so big.

    A GPS or LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY (if Google Play Services is used) is required to get a reasonably good location. This does require android.permission.ACCESS_FINE_LOCATION, however unless you only require km level accuracy, otherwise this permission is a must.

    Finally note that using Google Play Services with LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY I can get location data as accurate as 10m without turning on GPS, so this should still satisfy your requirement.

    Below is a complete example:

    AndroidManifest.xml

    
    
    

    MainActivity.java

    import android.app.Activity;
    import android.location.Location;
    import android.os.Bundle;
    import android.widget.TextView;
    
    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
    import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
    import com.google.android.gms.location.FusedLocationProviderApi;
    import com.google.android.gms.location.LocationRequest;
    import com.google.android.gms.location.LocationServices;
    
    public class MainActivity extends Activity implements
            com.google.android.gms.location.LocationListener, ConnectionCallbacks,
            OnConnectionFailedListener {
        private final FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
        private GoogleApiClient mGoogleAPIClient;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mGoogleAPIClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API).addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this).build();
        }
    
        @Override
        protected void onResume() {
            super.onResume();
    
            mGoogleAPIClient.connect();
        }
    
        @Override
        protected void onPause() {
            super.onPause();
    
            if (mGoogleAPIClient != null) {
            mGoogleAPIClient.disconnect();
            }
        }
    
        @Override
        public void onConnected(Bundle arg0) {
            final LocationRequest locationRequest = LocationRequest.create();
            locationRequest
                    .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
            locationRequest.setInterval(30 * 1000);
            locationRequest.setFastestInterval(5 * 1000);
            fusedLocationProviderApi.requestLocationUpdates(mGoogleAPIClient,
                    locationRequest, this);
        }
    
        @Override
        public void onConnectionSuspended(int arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onConnectionFailed(ConnectionResult arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void onLocationChanged(Location location) {
            // the location is no more than 10 min old, and with reasonable
            // accurarcy (50m), done
            if (System.currentTimeMillis() < location.getTime() + 10 * 60 * 1000
                    && location.getAccuracy() < 50) {
                mGoogleAPIClient.disconnect();
                mGoogleAPIClient = null;
                ((TextView) findViewById(R.id.test)).setText(location.toString());
            }
        }
    }
    

提交回复
热议问题