码迷,mamicode.com
首页 > 移动开发 > 详细

android中gps的使用以及解析nmea0183协议

时间:2015-05-20 23:41:22      阅读:472      评论:0      收藏:0      [点我收藏+]

标签:

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

 

android中gps的使用以及解析nmea0183协议

标签:

原文地址:http://www.cnblogs.com/catcher92/p/4518391.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!