now i am trying to get my current location but i cant get it only one location 37.4219983,-122.084 this is location from any where i cant get my true location and this is my code

private void getDeviceLocation(){

    mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    try{
        if(mLocationPermissionsGranted){

            final Task location = mFusedLocationProviderClient.getLastLocation();
            location.addOnCompleteListener(new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    if(task.isSuccessful()){
                        Location currentLocation = (Location) task.getResult();
                        current_lat=currentLocation.getLatitude();
                        current_lng=currentLocation.getLongitude();
                        Log.e("currentLocation",current_lat+"....."+current_lng);
                        moveCamera(new LatLng(current_lat,current_lng),
                                15f);
                        mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                            @Override
                            public void onMapClick(LatLng latLng) {
                                mMap.clear();
                                MarkerOptions markerOptions=new MarkerOptions();
                                markerOptions.position(new LatLng(latLng.latitude,latLng.longitude)).title("disance");
                                float[]results=new float[10];
                                Location.distanceBetween(current_lat,current_lng,latLng.latitude,latLng.longitude,results);
                                markerOptions.snippet("ditance ="+results[0]);
                                mMap.addMarker(markerOptions);
                            }
                        });
                        mMap.clear();


                    }else{
                        Toast.makeText(add_center_map.this, "unable to get current location", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }catch (SecurityException e){
    }
}

private void moveCamera(LatLng latLng, float zoom){
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
}
Answer

You are probably testing this in emulator. Its location is set to

Longitude: -122.0840 Latitude: 37.4220 Altitude: 0.0

by default.

You can test on a real device or configure other coordinates for your virtual device in its Extended controls (three dots beside the AVD).

  • 1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions