how to get current location in google map android

前端 未结 17 1980
长发绾君心
长发绾君心 2020-11-27 14:04

Actually my problem is I am not getting current location latitude and longitude I tried so many ways.I know that this question already asked in SO I tried that answers also

17条回答
  •  鱼传尺愫
    2020-11-27 14:53

    All solution mentioned above using that code which are deprecated now!Here is the new solution

    1. Add implementation 'com.google.android.gms:play-services-places:15.0.1' dependency in your gradle file

    2. Add network permission in your manifest file:

    1. Now use this code to get current location

        FusedLocationProviderClient mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
      
        mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener() {
                      @Override
                      public void onSuccess(Location location) {
                          // GPS location can be null if GPS is switched off
                              currentLat = location.getLatitude();
                              currentLong = location.getLongitude();
                              Toast.makeText(HomeNavigationBarActivtiy.this, "lat " + location.getLatitude() + "\nlong " + location.getLongitude(), Toast.LENGTH_SHORT).show();
                      }
                  })
                  .addOnFailureListener(new OnFailureListener() {
                      @Override
                      public void onFailure(@NonNull Exception e) {
                          e.printStackTrace();
                      }
                  });
      

提交回复
热议问题