Showing a desired location on Google Maps in Android?

By | December 20, 2011

In the previous post I showed you how to start with google Maps and changing the views and changing the zooming of maps.
Today I will show how to show our desired location in the Map.
For that we need the latitude and longitude of the location we want to show the location on the map.

The only change is in the java code of the previous post.
here is the Modified code

package pack.coderzheaven;

import android.os.Bundle;
import android.view.KeyEvent;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class MapDemo extends MapActivity {

	MapView map_view;
	MapController mc;
	GeoPoint p;

    @Override
    public void onCreate(Bundle savedInstanceState) {

    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        map_view = (MapView)findViewById(R.id.mv);
        map_view.setBuiltInZoomControls(true);    // Show the Zoom Controls
        //map_view.setSatellite(true);
        /** You can also display the map in street view, using the setStreetView() method: **/
        //map_view.setStreetView(true);

        mc = map_view.getController();
        String coordinates[] = {"28.38", "77.12"};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);

        p = new GeoPoint(
            (int) (lat * 1E6),
            (int) (lng * 1E6));

        mc.animateTo(p);
        mc.setZoom(10);
        map_view.invalidate();

    }

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

	public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        MapController mc = map_view.getController();
        switch (keyCode)
        {
            case KeyEvent.KEYCODE_1:
                mc.zoomIn();
                break;
            case KeyEvent.KEYCODE_2:
                mc.zoomOut();
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}

Note : Replace the API key with yours in the XML as I have mentioned in the previous post.

Showing a location on Google Maps

Showing a location on Google Maps

Please don’t forget to add your valuable comments on this post, because comments are our encouragements for future posts.

Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Reply

Your email address will not be published. Required fields are marked *