5

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.

Brais Gabin
  • 5,827
  • 6
  • 57
  • 92
User358218
  • 523
  • 3
  • 8
  • 25

3 Answers3

9

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

ggomeze
  • 5,711
  • 6
  • 29
  • 32
  • 1
    An afterthought: This is unsafe as you are modifying the UI whilst not on that thread. Look into wrapping it in something like this: http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29 – Ryan Sep 03 '11 at 15:45
3

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();

}
NickT
  • 23,844
  • 11
  • 78
  • 121
  • This doesn't work. After i send the lat and long values, the marker on the map moves but it doesnt automatically move to the marker. – User358218 Nov 10 '10 at 17:21
  • I didn't know that you had a marker. This makes the map centre to your current location. If the first answer does what you need, then stick with it. – NickT Nov 10 '10 at 17:29
  • No i don't have a marker. But my code places an overlay when i send the lat and long over. What i would like to find out is how would i make the android phone load my current location when i start up the program. – User358218 Nov 10 '10 at 17:31
  • I enable the gps with lm = (LocationManager) getSystemService(LOCATION_SERVICE); then lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100, this); (30 secs, 100 metres) - this enables the location listener – NickT Nov 10 '10 at 17:43
  • So i have to add lm = (LocationManager) getSystemService(LOCATION_SERVICE); then lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 100, this); (30 secs, 100 metres) - in the onLocationChanged? – User358218 Nov 10 '10 at 17:45
  • No, put that outside the listener, somewhere like onCreate() – NickT Nov 10 '10 at 17:46
  • Have a look at http://www.vogella.de/articles/AndroidLocationAPI/article.html This is a tutorial with a locationListener in. Some tutorials don't have that. – NickT Nov 10 '10 at 17:51
  • What version of the API is this? MapView doesn't have an onLocationChanged(). – Stealth Rabbi Oct 11 '12 at 15:05
1

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

Thorsten Niehues
  • 13,712
  • 22
  • 78
  • 113