Android studio location

1.7k views Asked by At

Iv made a location app, when I open it it opens on a map of the world and I have to hit the target icon thing in the top right hand corner for it to zoom into user location, how do I make it automatically zoom to user location when app is opened?

2

There are 2 answers

0
A.S On
LocationManager locationManager = (LocationManager) getActivity()
                .getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();

        Location location = locationManager
                .getLastKnownLocation(locationManager.getBestProvider(criteria,
                        false));
        if (location != null) {

            googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(d.getLatitude(), d.getLongitude()), 13));

            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(new LatLng(location.getLatitude(), location.getLongitude())) // Sets
                                                                            // the
                                                                            // center
                                                                            // of
                                                                            // the
                                                                            // map
                                                                            // to
                                                                            // location
                                                                            // user
                    .zoom(14) // Sets the zoom
                    .bearing(0) // Sets the orientation of the camera to east
                    .tilt(40) // Sets the tilt of the camera to 30 degrees
                    .build(); // Creates a CameraPosition from the builder
            googleMap.animateCamera(CameraUpdateFactory
                    .newCameraPosition(cameraPosition));
            // change marker you


        }
0
Shvet On

You can use CameraPosition to show users location.

CameraPosition cameraPosition = new CameraPosition.Builder ()
                    .target (curentpoint).zoom (10f).tilt (70).build ();

Where target is current LatLog getting from either gps or network. zoom will Zoom map to 10. tilt will change map's angle.

after that use animateCamera method to add cameraposition.

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

I hope this is what you want.