package com.samples.location.mapviewoverlay;

import java.io.IOException;
import java.util.List;
import java.util.Locale;


import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MapView.LayoutParams;
import com.samples.location.mapviewoverlay.R;

import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MapViewActivity extends MapActivity {
	
	private MapController controller;
	private TextView text;
	private MapView map;
	
	private final LocationListener locListener = new LocationListener() {
		@Override
		public void onLocationChanged(Location location) {
		    printLocation(location);
		}

		@Override
		public void onProviderDisabled(String arg0) { 
			printLocation(null);
		}

		@Override
		public void onProviderEnabled(String arg0) { }

		@Override
		public void onStatusChanged(String arg0, int arg1, Bundle arg2) { }
	};
		  	  
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);

	    map = (MapView)findViewById(R.id.map);
	    text = (TextView)findViewById(R.id.text);

	    controller = map.getController();
	    controller.setZoom(17);
	    
	    map.setSatellite(true);
	    map.setStreetView(true);
	    map.displayZoomControls(false);
	    map.setBuiltInZoomControls(true);

	    LocationManager locationManager = 
	    	(LocationManager)getSystemService(Context.LOCATION_SERVICE);
	    locationManager.requestLocationUpdates(
	    		LocationManager.GPS_PROVIDER, 2000, 10, locListener);
	    printLocation(locationManager.getLastKnownLocation(
	    		LocationManager.GPS_PROVIDER));   
	}
  
	private void printLocation(Location location) {	
		if (location != null)
		{
			double lat = location.getLatitude();
		    double lon = location.getLongitude();
		    
		    GeoPoint point = new GeoPoint(
		    		(int)(lat * 1E6), (int)(lon * 1E6));
		    controller.animateTo(point);	
		    
	        ImageView marker = new ImageView(getApplicationContext());
	        marker.setImageResource(R.drawable.star);
	        MapView.LayoutParams markerParams = new MapView.LayoutParams(
	                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 
	                point, MapView.LayoutParams.TOP_LEFT );
	        
	        map.addView(marker, markerParams);
	        
		    text.setText(
		    		String.format("Lat: %4.2f, Long: %4.2f", lat, lon));
		    
		    try {
		    	Geocoder geocoder = new Geocoder(this, Locale.getDefault());
		    	List<Address> addresses = 
		    		geocoder.getFromLocation(lat, lon, 1);
		    	
		    	if (addresses.size() > 0) {
		    		Address address = addresses.get(0);
	
		    		for(int i=0; i<address.getMaxAddressLineIndex(); i++) {
		    			text.append(", " + address.getAddressLine(i)); 
		    		}
		    		text.append(", " + address.getCountryName());
		    	}
		    } 
		    catch (IOException e) {
		    	Toast.makeText(getApplicationContext(), e.toString(), 
		    			Toast.LENGTH_LONG).show();
		    }
		    catch (Exception e) {
		    	Toast.makeText(getApplicationContext(), e.toString(), 
		    			Toast.LENGTH_LONG).show();
		    }
		}
		else {
			text.setText("Unable get location");
		}
	}

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