首页 技术 正文
技术 2022年11月10日
0 收藏 679 点赞 3,985 浏览 5162 个字

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

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很难达到想要的结果

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901