畢業設計中需要用到安卓的gps定位,總結一下這幾天學到的關於gps相關的。
為了測試,所以布局文件很簡單,只有兩個TextView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true" tools:context="com.catcher.testcompass.MainActivity" > <TextView android:id="@+id/tv_rgs84" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="高程" /> <ScrollView android:id="@+id/scrollView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/tv_rgs84" android:layout_marginTop="80dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_nmea" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="nmea" /> </LinearLayout> </ScrollView> </RelativeLayout>
具體代碼實現
package com.catcher.testcompass; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.hardware.GeomagneticField; import android.location.GpsStatus.NmeaListener; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.provider.Settings; import android.widget.TextView; import android.widget.Toast; public class SecondActivity extends Activity { private TextView tvWGS84, tvNmea; private LocationListener gpsListener; private LocationManager mLocationManager; private GeomagneticField gmfield; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); //顯示wgs84數據 tvWGS84 = (TextView) findViewById(R.id.tv_rgs84); //顯示nmea協議中數據 tvNmea = (TextView) findViewById(R.id.tv_nmea); mLocationManager = ((LocationManager) getSystemService(Context.LOCATION_SERVICE)); mLocationManager.addNmeaListener(new NmeaListener() { @Override public void onNmeaReceived(long timestamp, String nmea) { tvNmea.invalidate(); //此處以GPGGA為例 //$GPGGA,232427.000,3751.1956,N,11231.1494,E,1,6,1.20,824.4,M,-23.0,M,,*7E if (nmea.contains("GPGGA")) { String info[] = nmea.split(","); //GPGGA中altitude是MSL altitude(平均海平面) tvNmea.setText("正在使用的衛星數 " + info[7] + "\n海拔高度 " + info[9] + "\n地球橢球面相對大地水准面的高度 WGS84水准面划分 " + info[11]); } } }); gpsListener = new MyLocationListner(); } private class MyLocationListner implements LocationListener { @Override public void onLocationChanged(Location location) { tvWGS84.invalidate(); tvNmea.invalidate(); Double longitude = location.getLongitude(); float accuracy = location.getAccuracy(); Double latitude = location.getLatitude(); Double altitude = location.getAltitude();// WGS84 float bearing = location.getBearing(); gmfield = new GeomagneticField((float) location.getLatitude(), (float) location.getLongitude(), (float) location.getAltitude(), System.currentTimeMillis()); tvWGS84.setText("Altitude=" + altitude + "\nLongitude=" + longitude + "\nLatitude=" + latitude + "\nDeclination=" + gmfield.getDeclination() + "\nBearing=" + bearing + "\nAccuracy=" + accuracy); } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } } @Override protected void onPause() { super.onPause(); //退出Activity后不再定位 mLocationManager.removeUpdates(gpsListener); } @Override protected void onResume() { super.onResume(); //判斷gps是否可用 if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { Toast.makeText(this, "gps可用", Toast.LENGTH_LONG).show(); //開始定位 mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, gpsListener); }else{ Toast.makeText(this, "請打開gps或者選擇gps模式為准確度高", Toast.LENGTH_LONG).show(); //前往設置GPS頁面 startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } } }
AndroidManifest添加權限
<!-- 這個權限用於進行網絡定位 --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" ></uses-permission> <!-- 這個權限用於訪問GPS定位 --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" ></uses-permission>