how to get current location in google map android

前端 未结 17 1995
长发绾君心
长发绾君心 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:58

             import android.Manifest;
    import android.content.pm.PackageManager;
    import android.location.Address;
    import android.location.Geocoder;
    import android.location.Location;
    import android.os.Build;
    import android.os.Bundle;
    
    import androidx.annotation.RequiresApi;
    import androidx.core.app.ActivityCompat;
    import androidx.fragment.app.FragmentActivity;
    
    import com.google.android.gms.location.FusedLocationProviderClient;
    import com.google.android.gms.location.LocationListener;
    import com.google.android.gms.location.LocationServices;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.OnMapReadyCallback;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;
    import com.google.android.gms.tasks.OnSuccessListener;
    
    import java.io.IOException;
    import java.util.List;
    import java.util.Locale;
    
    import static android.Manifest.permission.ACCESS_FINE_LOCATION;
    
    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {
    
        private GoogleMap mMap;
        private FusedLocationProviderClient client;
        double latit;
        double longi;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_maps);
            // Obtain the SupportMapFragment and get notified when the map is ready to be used.
            SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
            mapFragment.getMapAsync(this);
            client = LocationServices.getFusedLocationProviderClient(this);
        }
    @RequiresApi(api = Build.VERSION_CODES.M)
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
    
    
    
            try {
                setupMap();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
     client = LocationServices.getFusedLocationProviderClient(this);
    
            if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    Activity#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 Activity#requestPermissions for more details.
                return;
            }
            client.getLastLocation()
                    .addOnSuccessListener(this, new OnSuccessListener() {
                        @Override
                        public void onSuccess(Location location) {
                            // Got last known location. In some rare situations this can be null.
                            if (location != null) {
    
                                //    local=findViewById(R.id.tv5);
    
                                double la=location.getLatitude();
                                double lo=location.getLongitude();
    
                                LatLng curre=new LatLng(la,lo);
                                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(curre,18));
    
                            }
                        }
                    });
    
    
    
        }
        }
    

提交回复
热议问题