歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android獲取GPS坐標

Android獲取GPS坐標

日期:2017/3/1 10:23:22   编辑:Linux編程

Android獲取GPS坐標:

  1. package an.android.application;
  2. import java.util.Iterator;
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.location.GpsSatellite;
  6. import android.location.GpsStatus;
  7. import android.location.Location;
  8. import android.location.LocationListener;
  9. import android.location.LocationManager;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.Toast;
  17. public class SpeedToll extends Activity {
  18. /** Called when the activity is first created. */
  19. private static final int Search = Menu.FIRST;
  20. private static final int Myloc = Menu.FIRST + 1;
  21. private static final int Exit = Menu.FIRST + 2;
  22. private LocationManager locationManager;
  23. private GpsStatus gpsstatus;
  24. @Override
  25. public void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. /* 加載main.xml Layout */
  28. setContentView(R.layout.desktop);
  29. /* 以findViewById()取得Button對象,並加入onClickListener */
  30. Button b1 = (Button) findViewById(R.id.button1);
  31. b1.setOnClickListener(new Button.OnClickListener()
  32. {
  33. public void onClick(View v)
  34. {
  35. /* new一個Intent對象,並指定要啟動的class */
  36. Intent intent = new Intent();
  37. intent.setClass(SpeedToll.this, Map.class);
  38. /* 調用一個新的Activity */
  39. startActivity(intent);
  40. /* 關閉原本的Activity */
  41. //SpeedToll.this.finish();
  42. }
  43. });
  44. Button b2 = (Button) findViewById(R.id.button1);
  45. b2.setOnClickListener(new Button.OnClickListener()
  46. {
  47. public void onClick(View v)
  48. {
  49. /* new一個Intent對象,並指定要啟動的class */
  50. Intent intent = new Intent();
  51. intent.setClass(SpeedToll.this, Map.class);
  52. /* 調用一個新的Activity */
  53. startActivity(intent);
  54. /* 關閉原本的Activity */
  55. //SpeedToll.this.finish();
  56. }
  57. });
  58. }
  59. public boolean onCreateOptionsMenu(Menu menu) {
  60. super.onCreateOptionsMenu(menu);
  61. menu.add(0, Search, Menu.NONE, "搜索");
  62. menu.add(0, Myloc, Menu.NONE, "定位");
  63. menu.add(0, Exit, Menu.NONE, "退出");
  64. return true;
  65. }//menu
  66. public boolean onOptionsItemSelected(MenuItem item)
  67. {
  68. //super.onOptionsItemSelected(item);
  69. switch(item.getItemId()){
  70. case Search:
  71. //Search();
  72. break;
  73. case Myloc:
  74. GetMyLocation();
  75. break;
  76. case Exit:
  77. //mLocationManager.removeUpdates(this); //關閉GPS
  78. this.finish();
  79. break;
  80. }
  81. return super.onOptionsItemSelected(item);
  82. }
  83. public boolean GetMyLocation(){
  84. //獲取到LocationManager對象
  85. locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
  86. //根據設置的Criteria對象,獲取最符合此標准的provider對象
  87. String currentProvider = locationManager.getProvider(LocationManager.GPS_PROVIDER).getName();
  88. //根據當前provider對象獲取最後一次位置信息
  89. Location currentLocation = locationManager.getLastKnownLocation(currentProvider);
  90. //如果位置信息為null,則請求更新位置信息
  91. if(currentLocation == null){
  92. locationManager.requestLocationUpdates(currentProvider, 0, 0, locationListener);
  93. }
  94. //增加GPS狀態監聽器
  95. locationManager.addGpsStatusListener(gpsListener);
  96. //直到獲得最後一次位置信息為止,如果未獲得最後一次位置信息,則顯示默認經緯度
  97. //每隔10秒獲取一次位置信息
  98. while(true){
  99. currentLocation = locationManager.getLastKnownLocation(currentProvider);
  100. if(currentLocation != null){
  101. Log.d("Location", "Latitude: " + currentLocation.getLatitude());
  102. Log.d("Location", "location: " + currentLocation.getLongitude());
  103. Toast.makeText(SpeedToll.this, "Latitude: " + currentLocation.getLatitude()+ " "
  104. +"location: " + currentLocation.getLongitude(), Toast.LENGTH_SHORT).show();
  105. break;
  106. }else{
  107. Log.d("Location", "Latitude: " + 0);
  108. Log.d("Location", "location: " + 0);
  109. }
  110. try {
  111. Thread.sleep(10000);
  112. } catch (InterruptedException e) {
  113. Log.e("Location", e.getMessage());
  114. }
  115. }
  116. return false;
  117. }
  118. private GpsStatus.Listener gpsListener = new GpsStatus.Listener(){
  119. //GPS狀態發生變化時觸發
  120. @Override
  121. public void onGpsStatusChanged(int event) {
  122. //獲取當前狀態
  123. gpsstatus=locationManager.getGpsStatus(null);
  124. switch(event){
  125. //第一次定位時的事件
  126. case GpsStatus.GPS_EVENT_FIRST_FIX:
  127. break;
  128. //開始定位的事件
  129. case GpsStatus.GPS_EVENT_STARTED:
  130. break;
  131. //發送GPS衛星狀態事件
  132. case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
  133. Toast.makeText(SpeedToll.this, "GPS_EVENT_SATELLITE_STATUS", Toast.LENGTH_SHORT).show();
  134. Iterable<GpsSatellite> allSatellites = gpsstatus.getSatellites();
  135. Iterator<GpsSatellite> it=allSatellites.iterator();
  136. int count = 0;
  137. while(it.hasNext())
  138. {
  139. count++;
  140. }
  141. Toast.makeText(SpeedToll.this, "Satellite Count:" + count, Toast.LENGTH_SHORT).show();
  142. break;
  143. //停止定位事件
  144. case GpsStatus.GPS_EVENT_STOPPED:
  145. Log.d("Location", "GPS_EVENT_STOPPED");
  146. break;
  147. }
  148. }
  149. };
  150. //創建位置監聽器
  151. private LocationListener locationListener = new LocationListener(){
  152. //位置發生改變時調用
  153. @Override
  154. public void onLocationChanged(Location location) {
  155. Log.d("Location", "onLocationChanged");
  156. }
  157. //provider失效時調用
  158. @Override
  159. public void onProviderDisabled(String provider) {
  160. Log.d("Location", "onProviderDisabled");
  161. }
  162. //provider啟用時調用
  163. @Override
  164. public void onProviderEnabled(String provider) {
  165. Log.d("Location", "onProviderEnabled");
  166. }
  167. //狀態改變時調用
  168. @Override
  169. public void onStatusChanged(String provider, int status, Bundle extras) {
  170. Log.d("Location", "onStatusChanged");
  171. }
  172. };
  173. }

主要有兩個文件,一個是主文件,一個是xml文件。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved