歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android通過GPS獲得經緯度

Android通過GPS獲得經緯度

日期:2017/3/1 10:41:53   编辑:Linux編程

真機不行,模擬器可以。手機有問題,先記錄代碼再說。

代碼如下:

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="basic.android.lesson26"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdk android:minSdkVersion="7"/>
  7. <application android:label="@string/app_name">
  8. <activity android:name=".TestMyGPS"
  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. </application>
  16. <!-- 粗略定位授權 -->
  17. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  18. <!-- 精細定位授權 -->
  19. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  20. </manifest>
main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <Button android:id="@+id/button1"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="ok"/>
  11. <TextView android:id="@+id/textView1"
  12. android:layout_width="fill_parent"
  13. android:layout_height="wrap_content"
  14. android:text="Hello World, MainActivity"
  15. />
  16. <TextView android:id="@+id/show_status"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. android:text="init"/>
  20. <TextView android:id="@+id/temp_text"
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:text="臨時"/>
  24. </LinearLayout>
TestMyGPS.java
  1. package basic.android.lesson26;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.location.Criteria;
  6. import android.location.Location;
  7. import android.location.LocationListener;
  8. import android.location.LocationManager;
  9. import android.os.Bundle;
  10. import android.provider.Settings;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16. public class TestMyGPS extends Activity {
  17. private static final String TAG = "TestMyGPS";
  18. Button mButton;
  19. TextView tv1;
  20. TextView mStatus;
  21. TextView mTemp;
  22. LocationManager mlm;
  23. LocationListener locationListener;
  24. String mFilter;
  25. @Override
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.main);
  29. // 定義UI組件
  30. mButton = (Button) findViewById(R.id.button1);
  31. tv1 = (TextView) findViewById(R.id.textView1);
  32. mStatus = (TextView) findViewById(R.id.show_status);
  33. mTemp = (TextView) findViewById(R.id.temp_text);
  34. mButton.setOnClickListener(new View.OnClickListener() {
  35. public void onClick(android.view.View view) {
  36. // 轉至 GPS 設置界面
  37. Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
  38. startActivityForResult(intent, 0);
  39. }
  40. });
  41. // 獲取LocationManager對象
  42. mlm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  43. // 定義Criteria對象
  44. // 獲取GPS信息提供者
  45. Criteria filter = getFilter();
  46. mFilter = mlm.getBestProvider(filter, true);
  47. // try {
  48. // mlm.setTestProviderEnabled(mFilter, true);
  49. // } catch (IllegalArgumentException e) {
  50. // String err = "IllegalArgumentException=" + e.getMessage();
  51. // Log.e(TAG, err);
  52. // Toast.makeText(this, err, Toast.LENGTH_LONG).show();
  53. // }
  54. // openGPS();
  55. gpsStatus();
  56. // 位置監聽器
  57. locationListener = new LocationListener() {
  58. // 當位置改變時觸發
  59. public void onLocationChanged(Location location) {
  60. updateLocation(location);
  61. Toast.makeText(TestMyGPS.this, "onLocationChanged=" + location, Toast.LENGTH_LONG).show();
  62. gpsStatus();
  63. mTemp.setText("onLocationChanged="+location);
  64. }
  65. // Provider失效時觸發
  66. public void onProviderDisabled(String arg0) {
  67. gpsStatus();
  68. mTemp.setText("onProviderDisabled=" + arg0);
  69. }
  70. // Provider可用時觸發
  71. public void onProviderEnabled(String arg0) {
  72. gpsStatus();
  73. mTemp.setText("onProviderEnabled=" + arg0);
  74. }
  75. // Provider狀態改變時觸發
  76. public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
  77. mTemp.setText("onStatusChanged=" + arg0);
  78. }
  79. };
  80. // 500毫秒更新一次,忽略位置變化
  81. mlm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 3, locationListener);
  82. }
  83. private void openGPS() {
  84. // if (mlm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
  85. // Toast.makeText(this, " 位置源已設置! ", Toast.LENGTH_SHORT).show();
  86. // return;
  87. // }
  88. //
  89. // Toast.makeText(this, " 位置源未設置! ", Toast.LENGTH_SHORT).show();
  90. }
  91. private Criteria getFilter() {
  92. Criteria criteria = new Criteria();
  93. // 設置定位精確度 Criteria.ACCURACY_COARSE 比較粗略, Criteria.ACCURACY_FINE則比較精細
  94. criteria.setAccuracy(Criteria.ACCURACY_FINE);
  95. // 設置是否需要海拔信息 Altitude
  96. criteria.setAltitudeRequired(false);
  97. // 設置是否需要方位信息 Bearing
  98. criteria.setBearingRequired(false);
  99. // 設置是否允許運營商收費
  100. criteria.setCostAllowed(true);
  101. // 設置對電源的需求
  102. criteria.setPowerRequirement(Criteria.POWER_LOW);
  103. return criteria;
  104. }
  105. private void gpsStatus() {
  106. if (mlm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
  107. mStatus.setText("GPS開啟");
  108. } else {
  109. mStatus.setText("GPS未開啟");
  110. Toast.makeText(this, " 位置源未設置! ", Toast.LENGTH_SHORT).show();
  111. // 轉至 GPS 設置界面
  112. Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
  113. startActivityForResult(intent, 0);
  114. }
  115. }
  116. // 更新位置信息
  117. private void updateLocation(Location location) {
  118. if (location != null) {
  119. tv1.setText("更新位置:" + location.toString() + "\n\t其中經度:" + location.getLongitude() + "\n\t其中緯度:"
  120. + location.getLatitude());
  121. } else {
  122. tv1.setText("更新位置失敗");
  123. }
  124. }
  125. @Override
  126. protected void onDestroy() {
  127. mlm.removeUpdates(locationListener);
  128. // mlm.setTestProviderEnabled(mFilter, false);
  129. super.onDestroy();
  130. }
  131. }
Copyright © Linux教程網 All Rights Reserved