package com.example.Maps012;

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

import com.google.android.gms.identity.intents.Address;

import android.app.Activity;
import android.content.Context;
import android.location.Geocoder;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

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


// http://javapapers.com/android/android-geocoding-to-get-latitude-longitude-for-an-address/

// inverse 
// http://javapapers.com/android/android-get-address-with-street-name-city-for-location-with-geocoding/

// http://www.androidauthority.com/get-location-address-android-app-628764/

// http://www.coders-hub.com/2015/02/advance-android-google-map-2-tutorial-with-example.html#.WRDkBdykJKI

// http://wptrafficanalyzer.in/blog/locating-user-input-address-in-google-maps-android-api-v2-with-geocoding-api/

//https://www.touraineverte.fr/google-apis/index.html

public class Geocoder_find_address extends Activity
{
    Button addressButton;
    TextView addressTV;
    TextView latLongTV;

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

        addressTV = (TextView) findViewById(R.id.addressTV);
        latLongTV = (TextView) findViewById(R.id.latLongTV);

        addressButton = (Button) findViewById(R.id.addressButton);
        addressButton.setOnClickListener(new View.OnClickListener() 
        {
	            @Override
	            public void onClick(View arg0) 
	            {
	            		EditText editText = (EditText) findViewById(R.id.addressET);
	            		String address = editText.getText().toString();
	
	            		GeocodingLocation locationAddress = new GeocodingLocation();
	            		locationAddress.getAddressFromLocation(address, getApplicationContext(), new GeocoderHandler());
	            }
        });

    }

    private class GeocoderHandler extends Handler 
    {
	        @Override
	        public void handleMessage(Message message) 
	        {
		            String locationAddress;
		           
		            switch (message.what)
		            {
		                case 1:
		                    Bundle bundle = message.getData();
		                    locationAddress = bundle.getString("address");
		                    break;
		                default:
		                    locationAddress = null;
		            }
		            
		            latLongTV.setText(locationAddress);
	        }
    }
}


class GeocodingLocation {

    private static final String TAG = "GeocodingLocation";
    
	static List<android.location.Address> Totoaddress;


    public static void getAddressFromLocation(final String locationAddress, final Context context, final Handler handler) 
    {
        Thread thread = new Thread()
        {
		            @Override
		            public void run()
		            {
		            	
		            		Geocoder geocoder = new Geocoder(context, Locale.getDefault());
		            		String result = null;
			                
		            		try 
			                {
			                    
			                    Totoaddress = geocoder.getFromLocationName(locationAddress,5);
			                    
			                    if (Totoaddress != null && Totoaddress.size() > 0) 
			                    {
			                    	
			                    	android.location.Address location= Totoaddress.get(0);
			                        StringBuilder sb = new StringBuilder();
			                        sb.append(location.getLatitude()).append("\n");
			                        sb.append(location.getLongitude()).append("\n");
			                        result = sb.toString();
			                    }
			                }
			                
			                catch (IOException e) 
			                {
			                    Log.e(TAG, "Unable to connect to Geocoder", e);
			                } 
			                
			                finally 
			                {
				                    Message message = Message.obtain();
				                    message.setTarget(handler);
				                    if (result != null) 
				                    {
				                        message.what = 1;
				                        Bundle bundle = new Bundle();
				                        result = "Address: " + locationAddress +
				                                "\n\nLatitude and Longitude :\n" + result;
				                        bundle.putString("address", result);
				                       message.setData(bundle);
				                    } 
				                    
				                    else 
				                    {
				                        message.what = 1;
				                        Bundle bundle = new Bundle();
				                        result = "Address: " + locationAddress +
				                                "\n Unable to get Latitude and Longitude for this address location.";
				                        bundle.putString("address", result);
				                        message.setData(bundle);
				                    }
				                    
				                    message.sendToTarget();
			                }
		            }
        };
        thread.start();
    }
}
