1

I am trying to display latitude and longitude values in a TextView, but while running the project the values are not displayed. Can anyone help me?,Thank you in advance. My code is below:
GeocodingActivity.class

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. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    t1=(TextView)findViewById(R.id.textView1);
    locman=(LocationManager) getSystemService(Context.LOCATION_SERVICE);
       handler=new Handler(){
        public void handle(Message msg){
                  switch(msg.what){
        case 1: 
            t1.setText((String)msg.obj);
            break;
        }

        }
loclis=new LocationListener(){

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

}
@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
    }
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
     }
    };


}

}
MohanRaj S
  • 1,958
  • 4
  • 30
  • 54
Ganesh R
  • 61
  • 6
  • 1
    Finally i got the output ,just i added the requestLocationupdate method and renamed the handle method to handleMessage then relocated the LocationListener class outside the onCreate method ,now it works fine,thanks for all those who helped me – Ganesh R Jun 19 '12 at 14:57

2 Answers2

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

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.


EDIT:

Second approach

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

}

Then your handle method can looks like this

public void handle(Message msg){
   t1.setText(msg.getData().getString("updateGpsData"));
}

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").

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • there is no setData method for Message (i.e,yourMessage)in your case – Ganesh R Jun 13 '12 at 15:21
  • setData is not static method, you should call it with instance so Message message; message.setData()... – Simon Dorociak Jun 13 '12 at 15:24
  • i updated the code ,no result .can u check MyCode i edited it – Ganesh R Jun 13 '12 at 15:41
  • so problem is elsewhere,Handler is generally used with Threads and you don't have Thread, have look at some tutorials with Handler. check [this](http://stackoverflow.com/questions/5316393/handler-looper-implementation-in-android) – Simon Dorociak Jun 13 '12 at 15:46
0

please use the String.valueOf method on your coordinates like this

String.valueO(location.getLatitude())+","+String.valueOf(location.getLongitude())
silly
  • 7,789
  • 2
  • 24
  • 37