Finding current location of the user in Android

后端 未结 3 1001
南笙
南笙 2020-11-29 14:07

I am new to Android and I am trying to develop an Android app which shows current location of the user. I am using Genymotion. Now I am using

mLocation=Loca         


        
3条回答
  •  感情败类
    2020-11-29 14:28

    posting simple solution with code.

    add permissions inside AndroidManifest.xml file

    
    
    

    if you app is marshmallow compatible then check run time permission.

    add dependency inside gradle file:

    compile 'com.google.android.gms:play-services:9.2.0'
    

    implements this two interface in you activity

    GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
    

    and

    create googleapiclient object like this in oncreate:

    mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
    

    and make this on start activity

    mGoogleApiClient.connect();
    

    here we go on result callback of location on this override method.

    public void onConnected(@Nullable Bundle bundle) {
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
            Log.e(TAG, "onConnected: " + String.valueOf(mLastLocation.getLatitude()) + ":" + String.valueOf(mLastLocation.getLongitude()));
        } else {
            Toast.makeText(getApplicationContext(), "Your Location Not Found", Toast.LENGTH_LONG).show();
        }
    }
    

    full code of activity is something like that:

    public class LocationActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
    
    private static final String TAG = LocationActivity.class.getSimpleName();
    
    private GoogleApiClient mGoogleApiClient;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
    
    
        // Create an instance of GoogleAPIClient.
        if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .build();
        }
    }
    
    @Override
    protected void onStart() {
    // connect googleapiclient
        mGoogleApiClient.connect();
        super.onStart();
    }
    
    @Override
    protected void onStop() {
    // disconnect googleapiclient
        mGoogleApiClient.disconnect();
        super.onStop();
    }
    
    @Override
    public void onConnected(@Nullable Bundle bundle) {
        Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {
        // here we go you can see current lat long.
            Log.e(TAG, "onConnected: " + String.valueOf(mLastLocation.getLatitude()) + ":" + String.valueOf(mLastLocation.getLongitude()));
        }
    }
    
    @Override
    public void onConnectionSuspended(int i) {
    
    }
    
    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    
    }}
    

    p.s. googleapiclient is not working in emulator without play services, so you have to test it on real devices. please make sure gps is enable on that device.

    if you want to use traditional method for get location then try this tutorial. http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

    ------UPDATE JUNE 15---------

    for latest apis check android google post: https://android-developers.googleblog.com/2017/06/reduce-friction-with-new-location-apis.html

提交回复
热议问题