实现android基于百度的定位 百度定位的精度比腾讯定位要高一点,但是百度定位的key获取比较麻烦。定位前请链接网络
案例源代码我已上传
http://download.csdn.net/detail/u012373815/9002009
主要步骤:
1. 导入腾讯定位所需jar包
2. 配置androidmanifest.xml权限 并添加腾讯定位的key
3.实现定位
一, 导入百度定位所需jar包
二, 配置androidmanifest.xml权限 并添加腾讯定位的key
androidmanifest.xml权限:
<!-- 定位的实现 -->
<!-- 通过GPS得到精确位置 -->
<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- 通过网络得到粗略位置 -->
<uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!-- 访问网络. 某些位置信息需要从网络服务器获取 -->
<uses-permissionandroid:name="android.permission.INTERNET"/>
<!-- 访问WiFi状态.需要WiFi信息用于网络定位 -->
<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>
<!-- 修改WiFi状态.发起WiFi扫描,需要WiFi信息用于网络定位 -->
<uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"/>
<!-- 访问网络状态, 检测网络的可用性.需要网络运营商相关信息用于网络定位 -->
<!-- 判断网络状态 -->
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- 访问网络的变化, 需要某些信息用于网络定位 -->
<uses-permissionandroid:name="android.permission.CHANGE_NETWORK_STATE"/>
<!-- 访问手机当前状态, 需要某些信息用于网络定位 -->
<uses-permissionandroid:name="android.permission.READ_PHONE_STATE"/>
百度定位的key 需要添加在</activity>之后</application>之前百度定位的key请上百度开发者平台获取
</activity>
<!-- meta-data需要写在application中 -->
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="3yoD80PlKwMoP9yHHvmhS77k"/> </application>
三, 实现定位
publicclassLocationActivity extends Activity{
private LocationClientmLocationClient;
private TextViewLocationResult;
private ButtonstartLocation;
@Override
protectedvoid onCreate(BundlesavedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
mLocationClient =new LocationClient(this.getApplicationContext());
mLocationClient.registerLocationListener(new MyLocationListener());
LocationResult =(TextView)findViewById(R.id.textView1);
startLocation =(Button)findViewById(R.id.addfence);
startLocation.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
// TODO Auto-generated method stub
InitLocation();
if(startLocation.getText().equals(getString(R.string.startlocation))){
mLocationClient.start();
startLocation.setText(getString(R.string.stoplocation));
}else{
mLocationClient.stop();
startLocation.setText(getString(R.string.startlocation));
}
}
});
}
@Override
protectedvoid onStop() {
//TODO Auto-generated method stub
mLocationClient.stop();
super.onStop();
}
privatevoid InitLocation(){
LocationClientOptionoption = newLocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式
//option.setCoorType(tempcoor);//返回的定位结果是百度经纬度,默认值gcj02
int span=1000;
option.setScanSpan(span);//设置发起定位请求的间隔时间为5000ms
option.setIsNeedAddress(true);
mLocationClient.setLocOption(option);
}
/**
* 实现实位回调监听
*/
publicclass MyLocationListener implementsBDLocationListener {
@Override
publicvoidonReceiveLocation(BDLocation location) {
//Receive Location
StringBuffersb = newStringBuffer(256);
sb.append("time : ");
sb.append(location.getTime());
sb.append("\nerror code : ");
sb.append(location.getLocType());
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){
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());
}
LocationResult.setText(sb.toString());
Log.i("dwtedx",sb.toString());
}
}
}