User358218
User358218

Reputation: 523

How would i make my google map zoom in to my current location?

Hi i would like to make my map zoom into my current location. This current location is defined currently by sending lat and long to the emulator. How would i go about doing this?

My current mapactivity.java

public class MapsActivity extends MapActivity {

    private MapView mapView;
    private MyLocationOverlay myLocOverlay;
    MapController mc;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mapactivity);
        initMap();
        initMyLocation();


    }

    /**
     * Initialise the map and adds the zoomcontrols to the LinearLayout.
     */
    private void initMap() {
        mapView = (MapView) findViewById(R.id.mapView);

        View zoomView = mapView.getZoomControls();
        LinearLayout myzoom = (LinearLayout) findViewById(R.id.zoom);
        myzoom.addView(zoomView);
        mapView.displayZoomControls(true);
        mapView.getController().setZoom(17);
    }

    /**
     * Initialises the MyLocationOverlay and adds it to the overlays of the map
     */
    private void initMyLocation() {
        myLocOverlay = new MyLocationOverlay(this, mapView);
        myLocOverlay.enableMyLocation();
        mapView.getOverlays().add(myLocOverlay);

    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

Thank you.

Upvotes: 5

Views: 11262

Answers (3)

Thorsten Niehues
Thorsten Niehues

Reputation: 14420

Since you have your location data already you can create a LatLng Object/Structure and use this line directly on the Map object:

map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15.0f));

I use the Android Google Maps API v2

Upvotes: 1

NickT
NickT

Reputation: 23873

You need to implement onLocationChanged(). In my code I've added the overlays here too. As a minimum you need to setCenter() with your current location

public void onLocationChanged(Location location) {

   List<Overlay> overlays = mapView.getOverlays();
   myLocOverlay = new MyLocationOverlay(this, mapView);
   overlays.add(myLocOverlay);
   myLocOverlay.enableMyLocation();*

   // definitely need what's below
   int lat = (int) (location.getLatitude() * 1E6);
   int lng = (int) (location.getLongitude() * 1E6);
   GeoPoint point = new GeoPoint(lat, lng);
   mc.setCenter(point);
   mapView.invalidate();

}

Upvotes: 3

ggomeze
ggomeze

Reputation: 5741

Try adding this at the end of your initMyLocation code:

controller = mapView.getController();
mMyLocOverlay.runOnFirstFix(new Runnable() {
            public void run() {
                controller.setZoom(17);
                controller.animateTo(mMyLocOverlay.getMyLocation());
            }
        });

You can remove this line from your initMap method:

mapView.getController().setZoom(17);

Let me know if that works

Ger

Upvotes: 9

Related Questions