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;
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。