10

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?

Community
  • 1
  • 1
Adz
  • 2,809
  • 10
  • 43
  • 61
  • map.moveCamera should work - I just used it in a project to do exactly what you're describing. – Jade McGough Dec 01 '13 at 20:34
  • It's still not working, do you have any other way of doing it please? – Adz Dec 01 '13 at 21:19
  • map.moveCamera will immediately move the camera to that location and zoom. map.animateCamera will animate the camera moving to that location and zoom. I suspect that the problem is somewhere else, but it's hard without seeing how that code is being called. You're finding and accessing map view correctly. Are you sure that that line of code is actually being reached? Are you seeing any errors in your logcat? – Jade McGough Dec 01 '13 at 21:28

4 Answers4

15

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

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
4
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));
}
sivaBE35
  • 1,876
  • 18
  • 23
1

In your XML file

<Button
   android:id="@+id/Bzoomin"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:onClick="onZoom"
   android:text="^" />

   <Button
       android:id="@+id/Bzoomout"
       style="?android:attr/buttonStyleSmall"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="center_vertical"
       android:onClick="onZoom"
       android:text="v" />'

In your Java file

 public void onZoom(View view)
 {
     if(view.getId() == R.id.Bzoomin)
     {
         mMap.animateCamera(CameraUpdateFactory.zoomIn());
     }
     if(view.getId() == R.id.Bzoomout)
     {
         mMap.animateCamera(CameraUpdateFactory.zoomOut());
     }
 }
Bart
  • 9,925
  • 7
  • 47
  • 64
Arpit Patel
  • 7,212
  • 5
  • 56
  • 67
1

For Kotlin

 val location = CameraUpdateFactory.newLatLngZoom(latlng, 15F)
 mMap.animateCamera(location)
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77