Implementation of three positioning methods on Android

Implementation of three positioning methods on Android

Source code introduction

Implementation of three positioning methods for Android: GPS, Baidu Positioning, and Amap Positioning.

Source code running screenshot

Code snippet:

  1. public   class MainActivity extends Activity implements OnClickListener{
  2.   
  3. private TextView mTextView;
  4. private Button gpsBtn, baiduBtn, amapBtn;
  5.       
  6. //GPS  
  7. private LocationManager gpsManager;
  8. //baidu  
  9. private LocationClient baduduManager;
  10. //amap  
  11. private LocationManagerProxy aMapManager;
  12.   
  13. @Override  
  14. protected   void onCreate(Bundle savedInstanceState) {
  15. super .onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. mTextView = (TextView) findViewById(R.id.text);
  18. gpsBtn = (Button) findViewById(R.id.gps);
  19. baiduBtn = (Button) findViewById(R.id.baidu);
  20. amapBtn = (Button) findViewById(R.id.amap);
  21.           
  22. gpsBtn.setOnClickListener( this );
  23. baiduBtn.setOnClickListener( this );
  24. amapBtn.setOnClickListener( this );
  25. }
  26.       
  27. @Override  
  28. public   void onClick(View v) {
  29. switch (v.getId()) {
  30. case R.id.gps:
  31. if (gpsBtn.getText().toString().equals( "Open GPS positioning" )) {
  32. startGps();
  33. gpsBtn.setText( "Stop GPS positioning" );
  34. } else {
  35. stopGps();
  36. gpsBtn.setText( "Turn on GPS positioning" );
  37. }
  38. break ;
  39. case R.id.baidu:
  40. if (baiduBtn.getText().toString().equals( "Open Baidu Positioning" )) {
  41. startBaidu();
  42. baiduBtn.setText( "Stop Baidu positioning" );
  43. } else {
  44. stopBaidu();
  45. baiduBtn.setText( "Open Baidu Positioning" );
  46. }
  47. break ;
  48. case R.id.amap:
  49. if (amapBtn.getText().toString().equals( "Open Amap positioning" )) {
  50. startAmap();
  51. amapBtn.setText( "Stop Amap positioning" );
  52. } else {
  53. stopAmap();
  54. amapBtn.setText( "Open Amap positioning" );
  55. }
  56. break ;
  57.   
  58. default :
  59. break ;
  60. }
  61. }
  62.   
  63. private   void startAmap() {
  64. aMapManager = LocationManagerProxy.getInstance( this );
  65. /*
  66. * mAMapLocManager.setGpsEnable(false);
  67. * New method added in version 1.0.2. Setting true means that GPS positioning is included in the hybrid positioning, and false means pure network positioning. The default value is true Location
  68. * API positioning uses a hybrid positioning method of GPS and network
  69. *, the first parameter is the positioning provider, the second parameter has a minimum time of 2000 milliseconds, the third parameter has a distance interval in meters, and the fourth parameter is the positioning listener
  70. */  
  71. aMapManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 2000 , 10 , mAMapLocationListener);
  72. }
  73.   
  74. private   void stopAmap() {
  75. if (aMapManager != null ) {
  76. aMapManager.removeUpdates(mAMapLocationListener);
  77. aMapManager.destroy();
  78. }
  79. aMapManager = null ;
  80. }
  81.       
  82. private   void startBaidu() {
  83. if (baduduManager == null ) {
  84. baduduManager = new LocationClient( this );
  85. //Positioning configuration  
  86. LocationClientOption option = new LocationClientOption();
  87. // Positioning mode selection, high precision, power saving, device only  
  88. option.setLocationMode(LocationMode.Hight_Accuracy);
  89. //Select the type of positioning coordinate system, gcj02, bd09ll, bd09  
  90. option.setCoorType( "gcj02" );
  91. // Positioning time interval  
  92. option.setScanSpan( 1000 );
  93. //Select the address to locate  
  94. option.setIsNeedAddress( true );
  95. baduduManager.setLocOption(option);
  96. //Callback for successful registration of positioning  
  97. baduduManager.registerLocationListener(mBdLocationListener);
  98. }
  99. baduduManager.start();
  100. }
  101.       
  102. private   void stopBaidu() {
  103. baduduManager.stop();
  104. }
  105.       
  106.   
  107. private   void startGps() {
  108. // Get the LocationManager object  
  109. gpsManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  110.           
  111. //provider can be GPS positioning, base station and WIFI positioning  
  112. String provider = gpsManager.getProvider(LocationManager.GPS_PROVIDER).getName();
  113.           
  114. //3000ms is the positioning interval, 10m is the distance change threshold, gpsListener is the callback interface  
  115. gpsManager.requestLocationUpdates(provider, 3000 , 10 , gpsListener);
  116. }
  117.       
  118. private   void stopGps() {
  119. gpsManager.removeUpdates(gpsListener);
  120. }
  121.   
  122. // Create a location listener  
  123. private LocationListener gpsListener = new LocationListener() {
  124.           
  125. //Called when the position changes  
  126. @Override  
  127. public   void onLocationChanged(Location location) {
  128. Log.e( "Location" , "onLocationChanged" );
  129. double latitude = location.getLatitude();
  130. double longitude = location.getLongitude();
  131. float speed = location.getSpeed();
  132. long time = location.getTime();
  133. String s = "latitude--->" + latitude
  134. + " longitude--->" + longitude
  135. + " speed--->" + speed
  136. + " time--->" + new Date(time).toLocaleString();
  137. mTextView.setText( "GPS location\n" + s);
  138. }
  139.   
  140. // Called when provider fails  
  141. @Override  
  142. public   void onProviderDisabled(String provider) {
  143. Log.e( "Location" , "onProviderDisabled" );
  144. }
  145.   
  146. //Called when provider is enabled  
  147. @Override  
  148. public   void onProviderEnabled(String provider) {
  149. Log.e( "Location" , "onProviderEnabled" );
  150. }
  151.   
  152. //Called when the state changes  
  153. @Override  
  154. public   void onStatusChanged(String provider, int status, Bundle extras) {
  155. Log.e( "Location" , "onStatusChanged" );
  156. }
  157. };
  158.       
  159. private BDLocationListener mBdLocationListener = new BDLocationListener() {
  160.           
  161. @Override  
  162. public   void onReceiveLocation(BDLocation location) {
  163. //Receive Location  
  164. StringBuffer sb = new StringBuffer( 256 );
  165. sb.append( "time : " );
  166. sb.append(location.getTime());
  167. sb.append( "\nerror code : " );
  168. sb.append(location.getLocType());
  169. sb.append( "\nlatitude : " );
  170. sb.append(location.getLatitude());
  171. sb.append( "\nlontitude : " );
  172. sb.append(location.getLongitude());
  173. sb.append( "\nradius : " );
  174. sb.append(location.getRadius());
  175. if (location.getLocType() == BDLocation.TypeGpsLocation){
  176. sb.append( "\nspeed : " );
  177. sb.append(location.getSpeed());
  178. sb.append( "\nsatellite : " );
  179. sb.append(location.getSatelliteNumber());
  180. sb.append( "\ndirection : " );
  181. sb.append( "\naddr : " );
  182. sb.append(location.getAddrStr());
  183. sb.append(location.getDirection());
  184. } else   if (location.getLocType() == BDLocation.TypeNetWorkLocation){
  185. sb.append( "\naddr : " );
  186. sb.append(location.getAddrStr());
  187. sb.append( "\noperationers : " );
  188. sb.append(location.getOperators());
  189. }
  190. mTextView.setText( "Baidu Positioning\n" + sb.toString());
  191. }
  192. };
  193.       
  194. private AMapLocationListener mAMapLocationListener = new AMapLocationListener() {
  195.           
  196. @Override  
  197. public   void onStatusChanged(String provider, int status, Bundle extras) {
  198.               
  199. }
  200.           
  201. @Override  
  202. public   void onProviderEnabled(String provider) {
  203.               
  204. }
  205.           
  206. @Override  
  207. public   void onProviderDisabled(String provider) {
  208.               
  209. }
  210.           
  211. @Override  
  212. public   void onLocationChanged(Location location) {
  213.               
  214. }
  215.           
  216. @Override  
  217. public   void onLocationChanged(AMapLocation location) {
  218. if (location != null ) {
  219. Double geoLat = location.getLatitude();
  220. Double geoLng = location.getLongitude();
  221. String cityCode = "" ;
  222. String desc = "" ;
  223. Bundle locBundle = location.getExtras();
  224. if (locBundle != null ) {
  225. cityCode = locBundle.getString( "citycode" );
  226. desc = locBundle.getString( "desc" );
  227. }
  228. String str = ( "Positioning successful: (" + geoLng + "," + geoLat + ")"  
  229. + "\nAccuracy:" + location.getAccuracy() + "meters"  
  230. + "\nLocation method:" + location.getProvider() + "\nLocation time:"  
  231. + new Date(location.getTime()).toLocaleString() + "\nCity code:"  
  232. + cityCode + "\nLocation Description:" + desc + "\nProvince:"  
  233. + location.getProvince() + "\nCity:" + location.getCity()
  234. + "\nDistrict (county):" + location.getDistrict() + "\nRegion code:" + location
  235. .getAdCode());
  236. mTextView.setText( "Gaode positioning\n" + str);
  237. }
  238. }
  239. };
  240.   
  241. }
Source code link: http://download..com/data/1968757

<<:  From 0 to 100——The History of Zhihu’s Architecture Changes

>>:  Making mobile development easier Mobile Developer Service Alliance (MDSA) is officially launched

Recommend

Street lamps can be transformed into car charging stations that can provide WiFi

Totem, a New York company, has designed a new typ...

Google Maps app for iOS brings three new useful features

According to foreign media reports, Google Maps a...

How can small startups achieve low-cost customer acquisition and viral fission?

This article is generally applicable to small com...

Troubleshooting and solutions for wild pointer issues in Dewu H5 container

1. Background After the release of Dewu iOS 4.9.x...

How do we control the rocket after it's launched?

I said before that rockets and carrier rockets ar...