申请百度key:http://lbsyun.baidu.com/
1、jar包下载地址:https://pan.baidu.com/s/1J-boj0ct9oJ8YjXMR8X4KA
下载并复制到libs下,Add As Library
如需获取SHA1值:https://blog.csdn.net/meixi_android/article/details/72547966
2、jar包复制到libs文件下,并Add As library
3、Java代码
static BDLocation lastLocation = null;
private LocationClient mLocClient;
public MyLocationListenner myListener = new MyLocationListenner();//自定义方法
public class MyLocationListenner implements BDLocationListener {@Overridepublic void onReceiveLocation(BDLocation location) {if (location == null) {return;}if (lastLocation != null) {if (lastLocation.getLatitude() == location.getLatitude() && lastLocation.getLongitude() == location.getLongitude()) {Log.d("map", "same location, skip refresh");// mMapView.refresh(); //need this refresh?return;}}String addrlg; //定位结果lastLocation = location;if (!TextUtils.isEmpty(lastLocation.getLocationDescribe())){addrlg = lastLocation.getLocationDescribe();}else if (lastLocation.hasAddr()) {addrlg = lastLocation.getAddrStr();}else {addrlg = "定位失败...";return;}
// String city = lastLocation.getCity();double lat = lastLocation.getLatitude();double lot = lastLocation.getLongitude();ShareUtil.sharedPstring("nowla",String.valueOf(lat));ShareUtil.sharedPstring("nowlo",String.valueOf(lot));ShareUtil.sharedPstring("nowad",addrlg);Log.i("lgq","............"+addrlg+"........"+lat+"......."+lot);
// Toast.makeText(MainActivity.this, addrlg+"........"+lat, Toast.LENGTH_SHORT).show();
// tv_bottom_bar_me.setText(addrlg);
// mBaiduMap.animateMapStatus(u);}}
附:
sb.append("\nlocType : ");// 定位类型
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());
sb.append("\nCountryCode : ");// 国家码
sb.append(location.getCountryCode());
sb.append("\nCountry : ");// 国家名称
sb.append(location.getCountry());
sb.append("\ncitycode : ");// 城市编码
sb.append(location.getCityCode());
sb.append("\ncity : ");// 城市
sb.append(location.getCity());
sb.append("\nDistrict : ");// 区
sb.append(location.getDistrict());
sb.append("\nStreet : ");// 街道
sb.append(location.getStreet());
sb.append("\naddr : ");// 地址信息
sb.append(location.getAddrStr());
sb.append("\nDirection(not all devices have value): ");
sb.append(location.getDirection());// 方向
sb.append("\nlocationdescribe: ");
sb.append(location.getLocationDescribe());// 位置语义化信息
sb.append("\nPoi: ");// POI信息
private void showMapWithLocationClient() {mLocClient = new LocationClient(this);mLocClient.registerLocationListener(myListener);LocationClientOption option = new LocationClientOption();option.setOpenGps(true);// open gpsoption.setCoorType("bd09ll");//可选,默认0,即仅定位一次,设置定时发起定位请求的间隔需要大于等于1000ms才是有效的/** 定位sdk提供2种定位模式,定时定位和app主动请求定位。* setScanSpan < 1000 则为 app主动请求定位;* setScanSpan >=1000,则为定时定位模式(setScanSpan的值就是定时定位的时间间隔))* 定时定位模式中,定位sdk会按照app设定的时间定位进行位置更新,定时回调定位结果。此种定位模式适用于希望获得连续定位结果的情况。* 对于单次定位类应用,或者偶尔需要一下位置信息的app,可采用app主动请求定位这种模式。*///option.setScanSpan(2000);//可选,设置是否需要地址信息,默认不需要option.setIsNeedAddress(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”option.setIsNeedLocationDescribe(true);//设置是否需要返回位置POI信息,可以在BDLocation.getPoiList()中得到数据option.setIsNeedLocationPoiList(true);//在网络定位时,是否需要设备方向 true:需要 ; false:不需要option.setNeedDeviceDirect(true);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死option.setIgnoreKillProcess(false);//可选,默认false,设置是否收集CRASH信息,默认收集option.SetIgnoreCacheException(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要option.setEnableSimulateGps(false);option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);mLocClient.setLocOption(option);mLocClient.start();
}
4、运行
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showMapWithLocationClient();
}
权限
<uses-permission android:name="android.permission.ACCESS_GPS" />
<!-- 获取精确gps位置 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- 获取粗略位置 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- 允许程序访问额外的定位提供者指令获取模拟定位信息 -->
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<!-- 网络链接 -->
<uses-permission android:name="android.permission.INTERNET" />
6.0以上版本获取权限:https://blog.csdn.net/meixi_android/article/details/82114026
配置百度服务到manifest
<serviceandroid:name="com.baidu.location.f" android:enabled="true"android:process=":remote">
</service>
配置key
<meta-dataandroid:name="com.baidu.lbsapi.API_KEY"android:value="qn3ZT2xT7yDgbIgkaPooGpDdMjziASZj" />