How to get coordinates of an address in android

public class MapActivity extends android.support.v4.app.FragmentActivity{

	private static final int MAP_ZOOM = 15;
	private String mAddress;
	private GoogleMap mMap;
	private double mLongitude;
	private double mlatitude;

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

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			mAddress = extras.getString("Address");
		}
		mMap = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map)).getMap();

		Log.v("ddd", mAddress);

		Geocoder geocoder = new Geocoder(this, Locale.getDefault());
                //To initialice list of address
		List<Address> addresses = null;
		try {
                        //To put the address in list
			addresses = geocoder.getFromLocationName(mAddress, 1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
                //Get Latitude and longitude
		Address address = addresses.get(0);
		mLongitude = address.getLongitude();
		mlatitude = address.getLatitude();
		showMarker(mlatitude, mLongitude);
		centerMapOnMyLocation();

	}

	//show the poi in map
	private void showMarker(double lat, double lng)
	{
	    mMap.addMarker(new MarkerOptions()
	        .position(new LatLng(mlatitude, mLongitude))
	        .title("Pais: EspaƱa"));
	}
	//Center the point in map
	private void centerMapOnMyLocation() {
			mMap.moveCamera(CameraUpdateFactory.newLatLng(getMyLatLng()));
			mMap.animateCamera(CameraUpdateFactory.zoomTo(MAP_ZOOM));
	}
	private LatLng getMyLatLng() {
		return new LatLng(mlatitude, mLongitude);
	}
}

Leave a Reply

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