package com.computertechtalks.geocoding3;
import java.io.IOException;
import java.util.List;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.util.Log;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements LocationListener{
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
TextView text2;
String provider;
protected String latitude,longitude;
protected boolean gps_enabled,network_enabled;
EditText edit;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLat = (TextView) findViewById(R.id.textview1);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
//For Getting location
edit = (EditText) findViewById(R.id.editText);
text = (TextView) findViewById(R.id.textView);
text2 = (TextView) findViewById(R.id.textview2);
Button findBtn = (Button) findViewById(R.id.button_find);
findBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String address = edit.getText().toString();
Geocoder geoCoder = new Geocoder(getApplicationContext());
try {
text2.append("Location is : "+address);
List<Address> locations = geoCoder.getFromLocationName(
address, 3);
for (Address a : locations) {
double latti = a.getLatitude();
double longi = a.getLongitude();
text.append(latti + "," + longi);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Not working");
}
}
});
// Find location stops here
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
txtLat = (TextView) findViewById(R.id.textview1);
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
}
@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}
}
No comments:
Post a Comment