当前位置:   article > 正文

Android 系统api实现定位及使用百度提供的api来实现定位_api快速定位controller位置

api快速定位controller位置

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

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

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


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


Android系统提供的API使用:

  1. LocationManager locationManager ;
  2. void getLocationInfo() {
  3. if(locationManager == null){
  4. locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  5. }
  6. Criteria criteria = new Criteria();
  7. criteria.setAccuracy(Criteria.ACCURACY_COARSE); // 设置为最大精度
  8. criteria.setAltitudeRequired(false); // 不要求海拔信息
  9. criteria.setCostAllowed(true);//是否允许付费
  10. criteria.setPowerRequirement(Criteria.POWER_LOW); // 对电量的要求
  11. criteria.setBearingRequired(false); // 不要求Bearing信息
  12. String bestProvider = locationManager.getBestProvider(criteria, true);
  13. IWLog.i(TAG, "bestProvider=" + bestProvider);
  14. Location location = locationManager.getLastKnownLocation(bestProvider);
  15. updateWithNewLocation(location);
  16. locationManager.requestLocationUpdates(bestProvider, 1000, 2, mLocationListener);//1秒,2米
  17. }
  18. LocationListener mLocationListener = new LocationListener() {
  19. @Override
  20. public void onLocationChanged(Location location) {
  21. if(locationManager != null){
  22. locationManager.removeUpdates(mLocationListener);//我这里,只需要定位一次就可以了
  23. }
  24. updateWithNewLocation(location);
  25. }
  26. @Override
  27. public void onStatusChanged(String provider, int status, Bundle extras) {
  28. IWLog.i(TAG, "onStatusChanged");
  29. }
  30. @Override
  31. public void onProviderEnabled(String provider) {
  32. IWLog.i(TAG, "onProviderEnabled");
  33. }
  34. @Override
  35. public void onProviderDisabled(String provider) {
  36. IWLog.i(TAG, "onProviderDisabled");
  37. }
  38. };
  39. private void updateWithNewLocation(Location location){
  40. if (location != null) {
  41. double latitude = location.getLatitude(); // 经度
  42. double longitude = location.getLongitude(); // 纬度
  43. //double altitude = location.getAltitude(); // 海拔
  44. ///mMainFragment.navLoadUrl(String.format(NavigationUrl.NEARBY_URL,longitude,latitude));
  45. IWLog.v(TAG, "latitude " + latitude + " longitude:" + longitude);
  46. ///UtilWidget.showToast(this, "Latitude :" + location.getLatitude()+""+"Longitude :" + location.getLatitude());
  47. }else{
  48. IWLog.v(TAG, "don't know location info");
  49. UtilWidget.showToast(this, "无法获取位置信息");
  50. }
  51. }


百度定位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中的代码:

  1. /**
  2. * 监听函数,又新位置的时候,格式化成字符串,输出到屏幕中
  3. */
  4. public class MyLocationListenner implements BDLocationListener {
  5. @Override
  6. public void onReceiveLocation(BDLocation location) {
  7. if (location == null)
  8. return ;
  9. StringBuffer sb = new StringBuffer(256);
  10. sb.append("time : ");
  11. sb.append(location.getTime());
  12. sb.append("\nerror code : ");
  13. sb.append(location.getLocType());
  14. sb.append("\nlatitude : ");
  15. sb.append(location.getLatitude());
  16. sb.append("\nlontitude : ");
  17. sb.append(location.getLongitude());
  18. sb.append("\nradius : ");
  19. sb.append(location.getRadius());
  20. if (location.getLocType() == BDLocation.TypeGpsLocation){
  21. sb.append("\nspeed : ");
  22. sb.append(location.getSpeed());
  23. sb.append("\nsatellite : ");
  24. sb.append(location.getSatelliteNumber());
  25. } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){
  26. // sb.append("\n省:");
  27. // sb.append(location.getProvince());
  28. // sb.append("\n市:");
  29. // sb.append(location.getCity());
  30. // sb.append("\n区/县:");
  31. // sb.append(location.getDistrict());
  32. sb.append("\naddr : ");
  33. sb.append(location.getAddrStr());
  34. }
  35. sb.append("\nsdk version : ");
  36. sb.append(mLocationClient.getVersion());
  37. sb.append("\nisCellChangeFlag : ");
  38. sb.append(location.isCellChangeFlag());
  39. //logMsg(sb.toString());
  40. Log.i(TAG, sb.toString());
  41. UtilWidget.cancelProgressDialog();
  42. updateWithNewLocation(location);
  43. }
  44. public void onReceivePoi(BDLocation poiLocation) {
  45. if (poiLocation == null){
  46. return ;
  47. }
  48. StringBuffer sb = new StringBuffer(256);
  49. sb.append("Poi time : ");
  50. sb.append(poiLocation.getTime());
  51. sb.append("\nerror code : ");
  52. sb.append(poiLocation.getLocType());
  53. sb.append("\nlatitude : ");
  54. sb.append(poiLocation.getLatitude());
  55. sb.append("\nlontitude : ");
  56. sb.append(poiLocation.getLongitude());
  57. sb.append("\nradius : ");
  58. sb.append(poiLocation.getRadius());
  59. if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){
  60. sb.append("\naddr : ");
  61. sb.append(poiLocation.getAddrStr());
  62. }
  63. if(poiLocation.hasPoi()){
  64. sb.append("\nPoi:");
  65. sb.append(poiLocation.getPoi());
  66. }else{
  67. sb.append("noPoi information");
  68. }
  69. //logMsg(sb.toString());
  70. }
  71. }
  72. //设置相关参数
  73. public void setLocationOption() {
  74. LocationClientOption option = new LocationClientOption();
  75. option.setOpenGps(true);// 打开gprs
  76. // option.setAddrType("all");// 返回的定位结果包含地址信息
  77. option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
  78. option.setScanSpan(5);// 设置发起定位请求的间隔时间为5000ms
  79. // //设置定位模式,小于1秒则一次定位;大于等于1秒则定时定位
  80. option.disableCache(true);// 禁止启用缓存定位
  81. // option.setPoiNumber(5); // 最多返回POI个数
  82. // option.setPoiDistance(1000); // poi查询距离
  83. // /option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息
  84. // option.setPriority(LocationClientOption.NetWorkFirst); //设置网络优先
  85. // option.setPriority(LocationClientOption.GpsFirst); //不设置,默认是gps优先
  86. mLocationClient.setLocOption(option);
  87. }
  88. @Override
  89. public void onCreate(Bundle savedInstanceState) {
  90. super.onCreate(savedInstanceState);
  91. setContentView(R.layout.activity_main_layout);
  92. //百度定位服务
  93. mLocationClient = new LocationClient(this);
  94. //myListener = new MyLocationListenner();
  95. setLocationOption();
  96. mLocationClient.registerLocationListener( new MyLocationListenner());
  97. }
  98. @Override
  99. protected void onDestroy() {
  100. super.onDestroy();
  101. mLocationClient.stop();
  102. }
  103. 在需要开户定位的地方执行 mLocationClient.start();


manifest.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.baidu.locTest" android:versionCode="1"
  4. android:versionName="1.0">
  5. <uses-sdk android:minSdkVersion="8" />
  6. <application
  7. android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=".mainActivity" android:configChanges="orientation|keyboardHidden"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. <service android:name="com.baidu.location.f" android:enabled="true"
  16. android:process=":remote">
  17. <!-- <intent-filter>
  18. <action android:name="com.baidu.location.service_v2.9"></action>
  19. </intent-filter>-->
  20. </service>
  21. </application>
  22. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
  23. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
  24. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
  25. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
  26. <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
  27. <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
  28. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
  29. <uses-permission android:name="android.permission.INTERNET" />
  30. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
  31. <uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
  32. <uses-permission android:name="android.permission.VIBRATE"></uses-permission>
  33. <uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
  34. </manifest>


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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/290529
推荐阅读
相关标签
  

闽ICP备14008679号