Google Maps v2 - set both my location and zoom in

163,023

Solution 1

You cannot animate two things (like zoom in and go to my location) in one google map?

From a coding standpoint, you would do them sequentially:

    CameraUpdate center=
        CameraUpdateFactory.newLatLng(new LatLng(40.76793169992044,
                                                 -73.98180484771729));
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);

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

Here, I move the camera first, then animate the camera, though both could be animateCamera() calls. Whether GoogleMap consolidates these into a single event, I can't say, as it goes by too fast. :-)

Here is the sample project from which I pulled the above code.


Sorry, this answer is flawed. See Rob's answer for a way to truly do this in one shot, by creating a CameraPosition and then creating a CameraUpdate from that CameraPosition.

Solution 2

It's possible to change location, zoom, bearing and tilt all in one go. It's 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=en-US#moving_the_camera

Solution 3

this is simple solution for your question

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

Solution 4

Try in this way -

public class SummaryMapActivity extends FragmentActivity implements LocationListener{

 private GoogleMap mMap;
 private LocationManager locationManager;
 private static final long MIN_TIME = 400;
 private static final float MIN_DISTANCE = 1000;

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

    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
        }
    }
    mMap.setMyLocationEnabled(true);

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this); 


}

@Override
public void onLocationChanged(Location location) {
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 15);
    mMap.animateCamera(cameraUpdate);
    locationManager.removeUpdates(this);

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}

Solution 5

You also could set both parameters like,

mMap.moveCamera( CameraUpdateFactory.newLatLngZoom(new LatLng(21.000000, -101.400000) ,4) );

This locates your map on a specific position and zoom. I use this on the setting up my map.

Share:
163,023
Dustin
Author by

Dustin

Updated on September 30, 2020

Comments

  • Dustin
    Dustin over 3 years

    My question is, does anyone know how to set google maps up, to open up both my location and in a zoomed-in view?

    Currently, the main view opens up to Africa, all the way zoomed out.

    And so I have been searching for days now, and all I can find are:

    1) You cannot animate two things (like zoom in and go to my location) in one google map? So if I can figure out how to set the zoom before I set the animate, then this problem would be solved. That tends to be the issue, you can change one, but not both.

    2) I have found other classes that might be useful, but there is no help on how to set up the code so the class can manipulate the google map.

    This is the code I have been holding on to so far, some works, some do not. Some I thought might be useful later on.

    package com.MYWEBSITE.www;
    
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.SupportMapFragment;
    import com.google.android.gms.maps.model.LatLng;
    import android.content.Context;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.view.Menu;
    
    public class MainActivity extends FragmentActivity {
    private GoogleMap map;  
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
    
        map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
        map.setMyLocationEnabled(true);
    
        //LocationSource a = (LocationSource) getSystemService(Context.LOCATION_SERVICE);
        //LocationManager b = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //map.setLocationSource(a);
    
        Criteria criteria = new Criteria();
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);
        double lat =  location.getLatitude();
        double lng = location.getLongitude();
        LatLng coordinate = new LatLng(lat, lng);
    
        //CameraPosition.Builder x = CameraPosition.builder();
        //x.target(coordinate);
        //x.zoom(13);
    
        //Projection proj = map.getProjection();
        //Point focus = proj.toScreenLocation(coordinate);
    
        //map.animateCamera(CameraUpdateFactory.newLatLng(coordinate));
        map.animateCamera(CameraUpdateFactory.zoomBy(13));
        //map.moveCamera(CameraUpdateFactory.newLatLng(coordinate));
    
    
        ////LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;
    }
    }
    
  • Matthew
    Matthew over 8 years
    Should not be the accepted answer. Mubeen1992 answer is correct.
  • CommonsWare
    CommonsWare over 7 years
    @AlanHaverty: The sample app that I link to in the answer contains that code. It runs perfectly fine. I just tested it now on a Nexus 9 running Android 7.0, and a Nexus 6P running Android 6.0.1. It has run perfectly fine for a couple of years. It doesn't do them in one shot, and I agree that Rob's is a superior answer, as I noted in my answer. If you believe that you have evidence that this code does not work, post a sample project somewhere.
  • Alan
    Alan over 7 years
    @CommonsWare My apologies, while testing with your code my map's top left corner seemed to be set with the lat lng. I assumed you mistook what .newLatLng() did. I'm not sure why it was off, it must have been something else on my end, the api docs prove your case, sorry for going trigger happy on your answer! Deleting my first comment to avoid confusion.. API Source: developers.google.com/android/reference/com/google/android/g‌​ms/…