-1

This is my Activity:

import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.
    private LocationManager locationManager;
    private String provider;

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

        addMaracanazinho();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void addMaracanazinho()
    {
        LatLng pos = new LatLng(-34.642491, -58.642841);

        mMap.addMarker(new MarkerOptions()
                .title("Maracanazinho")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN))
                .position(pos));


    }

    private void setUpMapIfNeeded()
    {
        // Do a null check to confirm that we have not already instantiated the map.
        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.setMyLocationEnabled(true);

            }
        }
    }



}

I want that my app start with a zoom in my current location. I looked for some codes but noone have the answer that I want. Can someone tell me what I should add?

I find this: How would I make my google maps app start with zoom on my current location But I don't understand how put this in my code. Thanks and sorry for my bad english.

Community
  • 1
  • 1
facutopa
  • 76
  • 1
  • 10

3 Answers3

1

Used animateCamera for that like

mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
            latitude, longitude), 18.0f));
M D
  • 47,665
  • 9
  • 93
  • 114
1

change your setUpMapIfNeeded method to following and add the rest of code as well. Basically you need to register a LocationListener with the location manager of the system. You get a callback as onLocationChanged(Location l). This Location l is the users position, then you set this new location to your map. Simple.

     private void setUpMapIfNeeded()
        {
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);

            // Do a null check to confirm that we have not already instantiated the map.
            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.setMyLocationEnabled(true);

                }
            }
        }

    private LocationListener locationListener = new MyLocationListener(){
     @Override
            public void onLocationChanged(Location location) {
                // called when the listener is notified with a location update from the GPS
    LatLng userPosition = new LatLng(location.getLatitude(),
                    location.getLongitude());
              if (mMap != null){
                mMap .moveCamera(CameraUpdateFactory.newLatLngZoom(userPosition,
                        15));
                }

            }

            @Override
            public void onProviderDisabled(String provider) {
               // called when the GPS provider is turned off (user turning off the GPS on the phone)

            }

            @Override
            public void onProviderEnabled(String provider) {
               // called when the GPS provider is turned on (user turning on the GPS on the phone)
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
               // called when the status of the GPS provider changes
            }
    };
Thupten
  • 2,158
  • 1
  • 24
  • 31
1

In your MapsActivity file -> Change setUpMapIfNeeded() function:

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available.
    private LocationManager locationManager;
    private String provider;

...

private void setUpMapIfNeeded()
    {
        // Do a null check to confirm that we have not already instantiated the map.
        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.setMyLocationEnabled(true);

                locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                // Create a criteria object to retrieve provider
                Criteria criteria = new Criteria();

                // Get the name of the best provider
                String provider = locationManager.getBestProvider(criteria, true);

                // Get Current Location
                Location myLocation = locationManager.getLastKnownLocation(provider);

                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(new LatLng(myLocation.getLatitude(), myLocation.getLongitude())) 
                        .zoom(14).build();

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

            }
        }
}

You can also set the Zoom by setting the number (Here Zoom is 14).

arpit
  • 1,462
  • 1
  • 12
  • 12