毕业设计中需要用到安卓的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>

更多相关文章

  1. android 应用如何获取系统权限 以及root系统方法
  2. Android 运行时权限库
  3. Android Permission denied 错误 ( 附Android权限大全 )
  4. Android APK权限提升到System
  5. 高通android10.0默认赋予第三方apk权限
  6. Android控制手电筒代码,简单易用,不需要任何权限

随机推荐

  1. Anaconda环境配置
  2. Rocky Linux镜像在阿里云镜像站首发上线
  3. 自定义方法通过类名获取对象集合
  4. 一套有效应对技术面算法题的方法论
  5. Apache服务器是如何解析PHP 小编来给你解
  6. 2021年校招程序员之阿里的十轮面试问题真
  7. PHP高并发高可用系统以及面试分析
  8. Python(十)文件操作
  9. Linux SRE 必经之路
  10. 怎么画人物的眼睛?画漫画人物的眼睛画法