package com.example.geo_api;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

//@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity 
{
	EditText etLng;
	EditText etLat;
	GoogleMap googleMap;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		etLat = (EditText)findViewById(R.id.et_lat);
		etLng = (EditText)findViewById(R.id.et_lng);
		
		etLng.setOnEditorActionListener(new OnEditorActionListener() 
		{
			
				@Override
				public boolean onEditorAction(TextView v, int actionId, KeyEvent event) 
				{
						// ligne ci-dessous non fonctionnelle
						//if(actionId == EditorInfo.IME_ACTION_DONE)
						//{
							
							Toast.makeText(getApplicationContext(), "TRACE", Toast.LENGTH_LONG).show();
							
							String lat = etLat.getText().toString();
							String lng = etLng.getText().toString();
							
//					        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
		
//					        if(status!=ConnectionResult.SUCCESS)
//					        { 
//					            int requestCode = 10;
//					            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getParent(), requestCode);
//					            dialog.show();
//					        }
//					        else 
//					        { 
		
					            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
					            googleMap = fm.getMap();
					            googleMap.setMyLocationEnabled(true);
					            LatLng point = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
					            drawMarker(point);
					            
//					        }
					    //}
				
						return false;
				}
		});
	}
	
	private void drawMarker(LatLng point)
	{
		// Clears all the existing coordinates
		googleMap.clear();		
		
        // Creating an instance of MarkerOptions
        MarkerOptions markerOptions = new MarkerOptions();

        // Setting latitude and longitude for the marker
        markerOptions.position(point);       
        
        // Setting title for the InfoWindow
        markerOptions.title("Position");
        
        // Setting InfoWindow contents
        markerOptions.snippet("Latitude:"+point.latitude+",Longitude"+point.longitude);

        // Adding marker on the Google Map
        googleMap.addMarker(markerOptions);
        
        // Moving CameraPosition to the user input coordinates
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(point));

        // Setting the zoom level
        //googleMap.animateCamera(CameraUpdateFactory.zoomTo(10));        
        
    }
	

	
}
