Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Google Maps to automatically zoom in on my Current Location?

Possible duplicate of Google Maps v2 - set both my location and zoom in

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_g_maps);
        GoogleMap googleMap;
        LatLng myPosition;


        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){
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        LatLng latLng = new LatLng(latitude, longitude);
        myPosition = new LatLng(latitude, longitude);


    }

}
}

I've tried adding:

CameraUpdate center=
        CameraUpdateFactory.newLatLng(new LatLng(latitude,
                                                 longitude));
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

    googleMap.moveCamera(center);
    googleMap.animateCamera(zoom);

or

 googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude) ,4) );

but Google Maps doesn't automatically zoom in once I've hit the button which calls this method. I still get the Google UI which I can click to zoom in on my Location, but I want it to automatically zoom in.

Any help please?

like image 838
Adz Avatar asked Dec 01 '13 20:12

Adz


People also ask

How do I change the default zoom on Google Maps?

Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.


2 Answers

Try this is simple solution for your question

LatLng coordinate = new LatLng(lat, lng);
CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(coordinate, 5);
map.animateCamera(yourLocation);

and also..

It's possible to change location, zoom, bearing and tilt in one go. It is also possible to set the duration on the animatecamera call.

CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(MOUNTAIN_VIEW)      // Sets the center of the map to Mountain View
    .zoom(17)                   // Sets the zoom
    .bearing(90)                // Sets the orientation of the camera to east
    .tilt(30)                   // Sets the tilt of the camera to 30 degrees
    .build();                   // Creates a CameraPosition from the builder
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

Have a look at the docs here:

https://developers.google.com/maps/documentation/android/views?hl=fr-FR#moving_the_camera

like image 137
Shailendra Madda Avatar answered Oct 26 '22 17:10

Shailendra Madda


GoogleMap googleMap;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    googleMap = ((MapFragment) getFragmentManager().findFragmentById(
            R.id.map)).getMap();

    Location locationCt;
    LocationManager locationManagerCt = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationCt = locationManagerCt
            .getLastKnownLocation(LocationManager.GPS_PROVIDER);

    LatLng latLng = new LatLng(locationCt.getLatitude(),
            locationCt.getLongitude());
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    googleMap.addMarker(new MarkerOptions().position(latLng)
            .title("My Spot").snippet("This is my spot!")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_car)));

    googleMap.setMyLocationEnabled(true);

    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}
like image 21
sivaBE35 Avatar answered Oct 26 '22 17:10

sivaBE35