-1

I'm first time use googleMap so plz help me.I want to show user location on map when it open app. I have done this when user already open gps.

    LocationManager locationManagerCt = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);

    locationCt = locationManagerCt
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);
 LatLng latLng=new LatLng(locationCt.getLatitude(), locationCt.getLongitude());
            MarkerOptions marker = new MarkerOptions().position(latLng).title("Current Location").snippet("Discription");

            marker.icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));

           // Moving Camera to a Location with animation
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(latLng).zoom(15).build();

            HomeActivity.mGoogleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));

            HomeActivity.mGoogleMap.addMarker(marker);

when gps is turn off. I try to doing the same thing after turn on Gps. I'm turn on gps from here: https://stackoverflow.com/a/33555732/3393578

After user turn on gps

   @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == 1000) {
     if(resultCode == Activity.RESULT_OK){
         //after user press ok button on GPS popup 

         /*I've apply same code above but unable to get location
          its give me error on Location (nullpointer) */


     } if (resultCode == Activity.RESULT_CANCELED) {
     } 
} 

}

How to get location after turn on GPS within seconds. I don't want to refresh activity.

Thank you!

Community
  • 1
  • 1

3 Answers3

1

Firstly, switch to FusedLocationApi instead of LocationManager which is old now. You need to use requestLocationUpdates() method of FusedLocationAPI.

Go through the google samples for details: https://github.com/googlesamples/android-play-location

CoderP
  • 1,361
  • 1
  • 13
  • 19
0

Please use this library, this works really well

https://github.com/mrmans0n/smart-location-lib

Ismaran Duwadi
  • 1,529
  • 12
  • 10
0

Please try like this.

    private FusedLocationProviderClient mFusedLocationClient;
    LocationRequest mLocationRequest;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        ButterKnife.bind(this);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
}


        public void requestLocationUpdates() {
                mLocationRequest = new LocationRequest();
                mLocationRequest.setInterval(120000); // two minute interval
                mLocationRequest.setFastestInterval(1000);
                mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
                if (ContextCompat.checkSelfPermission(this,
                        Manifest.permission.ACCESS_FINE_LOCATION)
                        == PackageManager.PERMISSION_GRANTED) {
                    mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
                }
            }

    @Override
        public void onConnected(Bundle bundle) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#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 ActivityCompat#requestPermissions for more details.
                return;
            }
            requestLocationUpdates();
            mFusedLocationClient.getLastLocation()
                    .addOnCompleteListener(this, new OnCompleteListener<Location>() {
                        @Override
                        public void onComplete(@NonNull Task<Location> task) {
                            if (task.isSuccessful() && task.getResult() != null) {
                                mLastLocation = task.getResult();

                            } else {


                                Log.w(TAG, "getLastLocation:exception", task.getException());
                                Toast.makeText(mContext, "No Location Detected.", Toast.LENGTH_LONG).show();
                            }
                        }
                    });


        }
Kyaw Zin Htun
  • 950
  • 6
  • 9