user3702564
user3702564

Reputation: 41

Finding the current location of the user in android

SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
       googleMap = fm.getMap();
       googleMap.setMyLocationEnabled(true);
       LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
       Criteria criteria = new Criteria();
       String provider = locationManager.getBestProvider(criteria, true);

       Location location = locationManager.getLastKnownLocation(provider);

       if(location!=null){
           onLocationChanged(location);



       }
       locationManager.requestLocationUpdates(provider, 20000, 0, this);

I'm gettin location==null.Is there any other way to find it ..plz help Thanks in advance!!

Upvotes: 0

Views: 50

Answers (1)

Raviindra
Raviindra

Reputation: 114

if (googleMap != null) {


        googleMap .setMyLocationEnabled(true);

        Location myLoc = googleMap .getMyLocation();
        if (myLoc!= null) {

            double originlatitude = myLoc.getLatitude();
            double originlongitude = myLoc.getLongitude();
            map.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(originlatitude, originlongitude)));

                CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(originlatitude, originlongitude)).zoom(15).build();
                map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


             Toast.makeText(getApplicationContext(), originlatitude  +
             "\n" + originlongitude , Toast.LENGTH_SHORT).show();
    }   }

Upvotes: 1

Related Questions