开发者社区> 问答> 正文

android百度定位textview没有显示地址

public class StartCanteenActivity extends Activity
 {
 private TextView locationInfoTextView = null;
 private Button startButton = null;
 private LocationClient locationClient = null;
 private static final int UPDATE_TIME = 5000;
 private static int LOCATION_COUTNS = 0;
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main_canteen);
 locationInfoTextView = (TextView) this.findViewById(R.id.locationTextView);
 startButton = (Button) this.findViewById(R.id.location_button);
    locationClient = new LocationClient(this);
    //设置定位条件
    LocationClientOption option = new LocationClientOption();
    option.setOpenGps(true);        //是否打开GPS
    option.setCoorType("bd09ll");       //设置返回值的坐标类型
    option.setPriority(LocationClientOption.NetWorkFirst);  //设置定位优先级
    option.setScanSpan(UPDATE_TIME);    //设置定时定位的时间间隔。单位毫秒
    locationClient.setLocOption(option);

    //注册位置监听器
    locationClient.registerLocationListener(new BDLocationListener() {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if (location == null) {
                return;
            }
            StringBuffer sb = new StringBuffer(256);
            sb.append("\nAddress : ");
            sb.append(location.getAddrStr());
            sb.append(String.valueOf(LOCATION_COUTNS));
            locationInfoTextView.setText(sb.toString());
        }
    });
    startButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (locationClient == null) {
                return;
            }
            if (locationClient.isStarted()) {
                startButton.setText("Start");
                locationClient.stop();
                }
            else {
                startButton.setText("Stop");
                locationClient.start();
                locationClient.requestLocation();
            }
        }
    });
}
@Override
protected void onDestroy() {
    super.onDestroy();
    if (locationClient != null && locationClient.isStarted()) {
        locationClient.stop();
        locationClient = null;
    }

}


}

展开
收起
爵霸 2016-06-01 15:07:55 2223 0
1 条回答
写回答
取消 提交回答
  • 不要在布局文件中定义TextView,在代码中需要的时候new 出来添加进去就行了
    

    “答案来源于网络,供您参考”

    2019-09-25 15:15:43
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载