【问题标题】:Displaying latitude and longitude values in TextView在 TextView 中显示纬度和经度值
【发布时间】:2012-06-13 13:44:49
【问题描述】:

我试图在TextView 中显示纬度和经度值,但是在运行项目时,这些值不会显示。任何人都可以帮助我吗?,提前谢谢你。我的代码如下:
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) {
     }
    };


}

}

【问题讨论】:

  • 最后我得到了输出,只是我添加了requestLocationupdate方法并将handle方法重命名为handleMessage,然后将LocationListener类重新定位到onCreate方法之外,现在它工作正常,感谢所有帮助我的人

标签: android textview


【解决方案1】:
Message.obtain(handler,0,location.getLatitude()+","+location.getLongitude()).sendToTarget();

所以你必须意识到getLongitudegetLatitude 两种方法都返回Double。你需要String。 所以你需要使用String.valueOf()方法将它们转换为String

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

然后,Message.obj 返回Object,因此要从那里获取String,您应该将其转换为String 所以t1.setText((String) msg.obj);

现在,它应该可以工作了。


编辑:

第二种方法

@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();

}

那么你的handle 方法可以是这样的

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

所以您可以创建Bundle,并且您的更新数据只需添加到Bundle,然后使用setData() 为您的Message 设置数据,并在处理方法中调用getData().getString() 以检索您的更新GPS数据。

不要忘记 Bundlekey + value 有效,因此您将数据设置为具有特定密钥的 Bundle,因此您必须使用 same key ("updateGpsData")。

【讨论】:

  • 在你的情况下没有 setData 方法用于消息(即你的消息)
  • setData 不是静态方法,您应该使用实例调用它,以便消息消息; message.setData()...
  • 我更新了代码,没有结果。你能检查我的代码我编辑了它
  • 所以问题出在其他地方,Handler一般和Threads一起使用,你没有Thread,看一些Handler的教程。检查this
【解决方案2】:

请像这样在您的坐标上使用String.valueOf 方法

String.valueO(location.getLatitude())+","+String.valueOf(location.getLongitude())

【讨论】:

  • 它显示错误,我认为双值不能转换为字符串
  • 为什么不用""+(location.getLatitude()) 转换成字符串!!?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-24
相关资源
最近更新 更多