Ganesh R Ganesh R - 1 year ago 102
Android Question

Displaying latitude and longitude values in TextView

I am trying to display latitude and longitude values in a

, but while running the project the values are not displayed. Can anyone help me?,Thank you in advance. My code is below:

public class GeocodingActivity extends Activity {
LocationManager locman;
Criteria cri;
LocationProvider locpro;
AlertDialog.Builder builder;
Handler handler;
TextView t1;
LocationListener loclis;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
locman=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
handler=new Handler(){
public void handle(Message msg){
case 1:

loclis=new LocationListener(){

public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Message.obtain(handler, 1, arg0.getLatitude()+","+arg0.getLongitude()).sendToTarget();

public void onProviderDisabled(String provider) {

public void onProviderEnabled(String provider) {
public void onStatusChanged(String provider, int status,
Bundle extras) {



Answer Source

So you must realize that both methods getLongitude and getLatitude returns Double. And you need String. So you need to convert them to String with method String.valueOf()

Message.obtain(handler, 0, String.valueOf(location.getLatitude()) + "," + String.valueOf(location.getLongitude())).sendToTarget();

Then, Message.obj returns Object so for getting String from there you should cast it to String so t1.setText((String) msg.obj);

Now, it should works.


Second approach

public void onLocationChanged(Location location) {
   Bundle bundle = new Bundle();
   String data = String.valueOf(location.getLatitude()) + "," + String.valueOf(location.getLongitude());
   bundle.putString("updateGpsData", data);
   yourMessage.obtain(handler, 0).sendToTarget();


Then your handle method can looks like this

public void handle(Message msg){

So you can create Bundle and your updated data just add to Bundle and then with setData() you set data for your Message and in handle method you call getData().getString() for retrieve your update GPS data.

Don't forget that Bundle works on pair key + value so you set data to your Bundle with specific key so for get data you have to use same key ("updateGpsData").