Reputation: 2849
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?
Upvotes: 10
Views: 45134
Reputation: 13129
For Kotlin
val location = CameraUpdateFactory.newLatLngZoom(latlng, 15F)
mMap.animateCamera(location)
Upvotes: 1
Reputation: 1891
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));
}
Upvotes: 4
Reputation: 8047
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());
}
}
Upvotes: 1
Reputation: 21531
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
Upvotes: 15