1.获取百度密钥
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.TextView;
importcom.baidu.location.LocationClient;
importcom.baidu.location.LocationClientOption;
importcom.baidu.location.LocationClientOption.LocationMode;
publicclass MainActivity extendsActivity{
privateLocationClient mLocationClient;//定位SDK的核心类
privateTextView LocationResult;
privateButton startLocation;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationClient = ((LocationApplication)getApplication()).mLocationClient;
LocationResult = (TextView)findViewById(R.id.textView1);
((LocationApplication)getApplication()).mLocationResult = LocationResult;//调用LocationApplication,获得需要的信息
startLocation = (Button)findViewById(R.id.addfence);
startLocation.setOnClickListener(newOnClickListener() {
publicvoid onClick(View v) {
InitLocation();//初始化
if(startLocation.getText().equals("开启定位")){
mLocationClient.start();
startLocation.setText("停止定位");
}else{
mLocationClient.stop();
startLocation.setText("开启定位");
}
}
});
}
@Override
protectedvoid onStop() {
mLocationClient.stop();
super.onStop();
}
privatevoid InitLocation(){
LocationClientOption option = newLocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);//设置高精度定位定位模式
option.setCoorType("bd09ll");//设置百度经纬度坐标系格式
option.setScanSpan(1000);//设置发起定位请求的间隔时间为1000ms
option.setIsNeedAddress(true);//反编译获得具体位置,只有网络定位才可以
mLocationClient.setLocOption(option);
}
}
2定义LocationApplication类:目的是获取信息,定位额关键就靠它了
packagecom.example.getcoord_text;
importandroid.app.Application;
importandroid.util.Log;
importandroid.widget.TextView;
importcom.baidu.location.BDLocation;
importcom.baidu.location.BDLocationListener;
importcom.baidu.location.LocationClient;
publicclass LocationApplication extendsApplication {
publicLocationClient mLocationClient;//定位SDK的核心类
publicMyLocationListener mMyLocationListener;//定义监听类
publicTextView mLocationResult,logMsg;
@Override
publicvoid onCreate() {
super.onCreate();
mLocationClient = newLocationClient(this.getApplicationContext());
mMyLocationListener = newMyLocationListener();
mLocationClient.registerLocationListener(mMyLocationListener);
}
/**
* 实现实位回调监听
*/
publicclass MyLocationListener implementsBDLocationListener {
publicvoid onReceiveLocation(BDLocation location) {
//Receive Location
StringBuffer sb = newStringBuffer(256);
String city=location.getCity();
sb.append("time : ");
sb.append(location.getTime());//获得当前时间
sb.append("\nerror code : ");
sb.append(location.getLocType());//获得erro code得知定位现状
sb.append("\nlatitude : ");
sb.append(location.getLatitude());//获得纬度
sb.append("\nlontitude : ");
sb.append(location.getLongitude());//获得经度
sb.append("\nradius : ");
sb.append(location.getRadius());
if(location.getLocType() == BDLocation.TypeGpsLocation){//通过GPS定位
sb.append("\nspeed : ");
sb.append(location.getSpeed());//获得速度
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
sb.append("\ndirection : ");
sb.append("\naddr : ");
sb.append(location.getAddrStr());//获得当前地址
sb.append(location.getDirection());//获得方位
}elseif (location.getLocType() == BDLocation.TypeNetWorkLocation){//通过网络连接定位
sb.append("\naddr : ");
sb.append(location.getAddrStr());//获得当前地址
//运营商信息
sb.append("\noperationers : ");
sb.append(location.getOperators());//获得经营商?
}
logMsg(sb.toString());
Log.i("BaiduLocationApiDem", sb.toString());
}
}
/**
* 显示请求字符串
* @param str
*/
publicvoid logMsg(String str) {
try{
if(mLocationResult != null)
mLocationResult.setText(str); //所有的信息
mLocationResult.setText(city); //只有城市
}catch(Exception e) {
e.printStackTrace();
}
}
}
3.Mainfast.xml
1.在application中加上一个属性name
android:name="com.king.salebaby.Location.LocationApplication" //LocationApplication所在的包名
2.注册service
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote"
android:permission="android.permission.BAIDU_LOCATION_SERVICE" >
<intent-filter >
<action android:name="com.baidu.location.service_v2.6"></action>
</intent-filter>
</service>
百度定位获取当前信息
最新推荐文章于 2023-02-20 16:52:29 发布