Android开发学习03:定位获取当前地点的信息
界面布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/addfence"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/startlocation" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.89"
android:scrollbars="vertical"
android:text=" "
android:textColor="#ffffffff" />
</ScrollView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.89"
android:scrollbars="vertical"
android:text=" "
android:textColor="#ffffffff" />
</ScrollView>
</LinearLayout>一、获取定位服务的实例:
//OnStart
//定位服务启动后,回调获取相关定位结果/诊断信息
locationService = ((LocationService)getApplication()).LocationService();
//Intent from MainActivity to this: type=0
//Intent from LocationOption to this: type=1
//Activities 间传参获取信息
int type = getIntent().getIntExtra("from", 0);
if (type == 0) {//设置默认配置进行定位操作
locationService.setLocationOption(locationService.getDefaultLocationClientOption());
} else if (type == 1) {//直接启动服务
locationService.start();
}二、设置注册监听:
locationService.registerListener(mListener); //mListener中包含对定位信息与诊断信息回调的重载 //OnReceiveLocation()、OnDiagnoticMessage()
三、加载回调信息到用户界面的TextView中:
if (LocationResult != null) {
new Thread(new Runnable() {
@Override
public void run() {
LocationResult.post(new Runnable() {
@Override
public void run() {
if (tag == Utils.RECEIVE_TAG) {
LocationResult.setText(str);
} else if (tag == Utils.DIAGNOSTIC_TAG) {
LocationDiagnostic.setText(str);
}
}
});
}
}).start();
}四、重载方法
@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if (null != location && location.getLocType() != BDLocation.TypeServerError) {
int tag = Utils.RECEIVE_TAG;
StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
/**
* 时间也可以使用systemClock.elapsedRealtime()方法 获取的是自从开机以来,每次回调的时间;
* location.getTime() 是指服务端出本次结果的时间,如果位置不发生变化,则时间不变
*/
//此处把信息加入到stringbuffer,再LogMessage
logMsg(sb.toString(), tag);
}
}
@Override
public void onConnectHotSpotMessage(String s, int i) {
super.onConnectHotSpotMessage(s, i);
}
@Override
public void onLocDiagnosticMessage(int locType, int diagnosticType, String diagnosticMessage) {
super.onLocDiagnosticMessage(locType, diagnosticType, diagnosticMessage);
int tag = Utils.DIAGNOSTIC_TAG;
StringBuffer sb = new StringBuffer(256);
//此处把信息加入到stringbuffer,再LogMessage
logMsg(sb.toString(), tag);
}效果:

相关推荐
韩伟佳 2020-10-09
wuleihenbang 2020-09-16
zzqLivecn 2020-07-09
chenjinlong 2020-06-10
yinbaoshiguang 2020-06-09
sgafdsg 2020-06-04
ustcrding 2020-06-03
chenjinlong 2020-06-03
AndroidGA 2020-06-01
安辉 2020-05-27
绿豆饼 2020-05-26
CNETNews 2020-05-26
xilove0 2020-05-12
绿豆饼 2020-05-12
ChainDestiny 2020-05-07
doomvsjing 2020-05-07
hqulyc 2020-05-05
lyccsu 2020-04-30