Android 系统api实现定位及使用百度提供的api来实现定位

时间:2024-01-05 22:27:02

目前在国内使用定位的方法主要是

1. Android系统提供的 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

2. 百度提供的api(需要在应用程序中加入相应的.so,.jar包)百度提供的定位api,与你本机是否有装百度地图程序程序没有关系

下面简单介绍一下使用方法

Android系统提供的API使用:

LocationManager locationManager ;

	void getLocationInfo() {

		if(locationManager == null){
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE); // 设置为最大精度
criteria.setAltitudeRequired(false); // 不要求海拔信息
criteria.setCostAllowed(true);//是否允许付费
criteria.setPowerRequirement(Criteria.POWER_LOW); // 对电量的要求
criteria.setBearingRequired(false); // 不要求Bearing信息 String bestProvider = locationManager.getBestProvider(criteria, true);
IWLog.i(TAG, "bestProvider=" + bestProvider); Location location = locationManager.getLastKnownLocation(bestProvider);
updateWithNewLocation(location); locationManager.requestLocationUpdates(bestProvider, 1000, 2, mLocationListener);//1秒,2米 } LocationListener mLocationListener = new LocationListener() { @Override
public void onLocationChanged(Location location) { if(locationManager != null){
locationManager.removeUpdates(mLocationListener);//我这里,只需要定位一次就可以了
}
updateWithNewLocation(location); } @Override
public void onStatusChanged(String provider, int status, Bundle extras) {
IWLog.i(TAG, "onStatusChanged"); } @Override
public void onProviderEnabled(String provider) {
IWLog.i(TAG, "onProviderEnabled"); } @Override
public void onProviderDisabled(String provider) {
IWLog.i(TAG, "onProviderDisabled"); }
}; private void updateWithNewLocation(Location location){
if (location != null) {
double latitude = location.getLatitude(); // 经度
double longitude = location.getLongitude(); // 纬度
//double altitude = location.getAltitude(); // 海拔 ///mMainFragment.navLoadUrl(String.format(NavigationUrl.NEARBY_URL,longitude,latitude)); IWLog.v(TAG, "latitude " + latitude + " longitude:" + longitude);
///UtilWidget.showToast(this, "Latitude :" + location.getLatitude()+""+"Longitude :" + location.getLatitude());
}else{
IWLog.v(TAG, "don't know location info");
UtilWidget.showToast(this, "无法获取位置信息");
}
}

百度定位API

百度官方说明:http://api.map.baidu.com/lbsapi/cloud/geosdk-android.htm

下载包及demo:http://api.map.baidu.com/lbsapi/cloud/geosdk-android-download.htm

可以直接参考百度提供的demo来实现自己的定位

MainActivity中的代码:

/**
* 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
*/
public class MyLocationListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return ; StringBuffer sb = new StringBuffer(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());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
// sb.append("\n省:");
// sb.append(location.getProvince());
// sb.append("\n市:");
// sb.append(location.getCity());
// sb.append("\n区/县:");
// sb.append(location.getDistrict());
sb.append("\naddr : ");
sb.append(location.getAddrStr());
}
sb.append("\nsdk version : ");
sb.append(mLocationClient.getVersion());
sb.append("\nisCellChangeFlag : ");
sb.append(location.isCellChangeFlag());
//logMsg(sb.toString());
Log.i(TAG, sb.toString()); UtilWidget.cancelProgressDialog();
updateWithNewLocation(location);
} public void onReceivePoi(BDLocation poiLocation) {
if (poiLocation == null){
return ;
}
StringBuffer sb = new StringBuffer(256);
sb.append("Poi time : ");
sb.append(poiLocation.getTime());
sb.append("\nerror code : ");
sb.append(poiLocation.getLocType());
sb.append("\nlatitude : ");
sb.append(poiLocation.getLatitude());
sb.append("\nlontitude : ");
sb.append(poiLocation.getLongitude());
sb.append("\nradius : ");
sb.append(poiLocation.getRadius());
if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){
sb.append("\naddr : ");
sb.append(poiLocation.getAddrStr());
}
if(poiLocation.hasPoi()){
sb.append("\nPoi:");
sb.append(poiLocation.getPoi());
}else{
sb.append("noPoi information");
}
//logMsg(sb.toString());
}
} //设置相关参数
public void setLocationOption() {
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);// 打开gprs
// option.setAddrType("all");// 返回的定位结果包含地址信息
option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5);// 设置发起定位请求的间隔时间为5000ms
// //设置定位模式,小于1秒则一次定位;大于等于1秒则定时定位
option.disableCache(true);// 禁止启用缓存定位
// option.setPoiNumber(5); // 最多返回POI个数
// option.setPoiDistance(1000); // poi查询距离
// /option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息 // option.setPriority(LocationClientOption.NetWorkFirst); //设置网络优先
// option.setPriority(LocationClientOption.GpsFirst); //不设置,默认是gps优先
mLocationClient.setLocOption(option); }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_layout); //百度定位服务
mLocationClient = new LocationClient(this);
//myListener = new MyLocationListenner();
setLocationOption();
mLocationClient.registerLocationListener( new MyLocationListenner());
}
@Override
protected void onDestroy() {
super.onDestroy();
mLocationClient.stop(); }
在需要开户定位的地方执行 mLocationClient.start();



详细参数请到官试api上参考

最后想说的一句是,在国内可能使用百度定位更好一些。因为我用系统的api很难达到想要的结果