第一步: 获取密钥

应用名称可以随便填写
发布版SHA1 的获取要在

keytool -v -list -keystore: C:\Users\admin\.android\debug.keystore 按回车
获取 口令是android 输入时不可见 ,但是确实是输入了 按回车
可得到SHA1。
接下来下载最新

下载后是

把此文件复制到

然后在AndroidManifest中 记得配权限

环境以及配好
2:代码
1 public class MainActivity extends AppCompatActivity {
2
3 public LocationClient mLocationClient = null;
4 public BDLocationListener myListener = new MyLocationListener();
5
6 @Override
7 protected void onCreate(Bundle savedInstanceState) {
8 super.onCreate(savedInstanceState);
9 setContentView(R.layout.activity_main);
10
11 mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类
12 mLocationClient.registerLocationListener(myListener); //注册监听函数
13 initLocation();//初始化
14 mLocationClient.start();//开始定位
15 }
16
17 private void initLocation() {
18 LocationClientOption option = new LocationClientOption();
19 option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
20 );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
21 option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
22 int span = 1000;
23 option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
24 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
25 option.setOpenGps(true);//可选,默认false,设置是否使用gps
26 option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
27 option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
28 option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
29 option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
30 option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
31 option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要
32 mLocationClient.setLocOption(option);
33 }
34
35 public class MyLocationListener implements BDLocationListener {
36 @Override
37 public void onReceiveLocation(BDLocation location) {
38 //Receive Location
39 StringBuffer sb = new StringBuffer(256);
40 sb.append("time : ");
41 sb.append(location.getTime());
42 sb.append("\nerror code : ");
43 sb.append(location.getLocType());
44 sb.append("\nlatitude : ");
45 sb.append(location.getLatitude());
46 sb.append("\nlontitude : ");
47 sb.append(location.getLongitude());
48 sb.append("\nradius : ");
49 sb.append(location.getRadius());
50 if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
51 sb.append("\nspeed : ");
52 sb.append(location.getSpeed());// 单位:公里每小时
53 sb.append("\nsatellite : ");
54 sb.append(location.getSatelliteNumber());
55 sb.append("\nheight : ");
56 sb.append(location.getAltitude());// 单位:米
57 sb.append("\ndirection : ");
58 sb.append(location.getDirection());// 单位度
59 sb.append("\naddr : ");
60 sb.append(location.getAddrStr());
61 sb.append("\ndescribe : ");
62 sb.append("gps定位成功");
63
64 } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
65 sb.append("\naddr : ");
66 sb.append(location.getAddrStr());
67 //运营商信息
68 sb.append("\noperationers : ");
69 sb.append(location.getOperators());
70 sb.append("\ndescribe : ");
71 sb.append("网络定位成功");
72 } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
73 sb.append("\ndescribe : ");
74 sb.append("离线定位成功,离线定位结果也是有效的");
75 } else if (location.getLocType() == BDLocation.TypeServerError) {
76 sb.append("\ndescribe : ");
77 sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
78 } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
79 sb.append("\ndescribe : ");
80 sb.append("网络不同导致定位失败,请检查网络是否通畅");
81 } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
82 sb.append("\ndescribe : ");
83 sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
84 }
85 sb.append("\nlocationdescribe : ");
86 sb.append(location.getLocationDescribe());// 位置语义化信息
87 List<Poi> list = location.getPoiList();// POI数据
88 if (list != null) {
89 sb.append("\npoilist size = : ");
90 sb.append(list.size());
91 for (Poi p : list) {
92 sb.append("\npoi= : ");
93 sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
94 }
95 }
96 //输出信息
97 Log.d("BaiduLocationApiDem", sb.toString());
98 mLocationClient.stop();
99 }
100 }
101
102 }
然后日志打印就可以了
来源:https://www.cnblogs.com/rookie-26/p/5865385.html