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) {
}
};
}
}
So you must realize that both methods
getLongitudeandgetLatitudereturnsDouble. And you needString.So you need to convert them to
Stringwith methodString.valueOf()Then,
Message.objreturnsObjectso for gettingStringfrom there you should cast it toStringsot1.setText((String) msg.obj);Now, it should works.
EDIT:
Second approach
}
Then your
handlemethod can looks like thisSo you can create
Bundleand your updated data just add toBundleand then withsetData()you set data for yourMessageand in handle method you callgetData().getString()for retrieve your updateGPSdata.Don’t forget that
Bundleworks on pair key + value so you set data to yourBundlewith specific key so for get data you have to use same key (“updateGpsData”).