首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Android Things向TextView发送全球定位系统数据

Android Things向TextView发送全球定位系统数据
EN

Stack Overflow用户
提问于 2018-06-19 23:57:14
回答 1查看 187关注 0票数 1

我正在使用安卓的东西v1.0.1与Adafruit终极GPS突破板v3上的树莓派3。

我正在尝试在屏幕上显示GPS信息,特别是在显示屏上发送纬度、经度、高度和卫星数量到各个TextView上,但我正在努力。

我使用的NMEA驱动程序是:Github / Android Things / Contrib Drivers / GPS

这是我的MainActivity

代码语言:javascript
复制
public class MainActivity extends Activity {

private static final String UART_DEVICE_NAME = "UART0";

UartDevice mDevice;
NmeaGpsModule mGpsModule;
NmeaParser mGpsModuleCallback;
NmeaGpsDriver mGpsDriver;
LocationManager mLocMan;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setTitle("GPS Test");

    try {
        NmeaGpsDriver mGpsDriver = new NmeaGpsDriver(
                MainActivity.this,
                "UART0",
                9600,
                1.8f );

        mGpsDriver.register();

    } catch (IOException e) {
        // couldn't configure the gps driver...
        Log.w(TAG, "Couldn't configure the GPS driver");
    }

    mLocMan = (LocationManager) getSystemService(LOCATION_SERVICE);

    if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED) {
        Log.d(TAG, "No permission");
        return;
    }

    mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,
            0, 0, mLocList);
    mLocMan.registerGnssStatusCallback(mStatusCallback);
    mLocMan.addNmeaListener(mMessageListener);

    Button gpsbtn = (Button) findViewById(R.id.gpsbtn);
    gpsbtn.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {
            Log.i(TAG, "Updating Location...");
        }
    }
    );
}

    private LocationListener mLocList = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.i(TAG, "Latitude: " + location.getLatitude());
            Log.i(TAG, "Latitude: " + location.getLongitude());
            Log.i(TAG, "Altitute: " + location.getAltitude());

            TextView latbox = new TextView(MainActivity.this);
            latbox.findViewById(R.id.latbox);
            latbox.setText(location.getLatitude());

            TextView longbox = new TextView(MainActivity.this);
            longbox.findViewById(R.id.longbox);
            longbox.setText(location.getLongitude());

            TextView altbox = new TextView(MainActivity.this);
            altbox.findViewById(R.id.altbox);
            altbox.setText(location.getAltitude());
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) { }

        @Override
        public void onProviderEnabled(String provider) { }

        @Override
        public void onProviderDisabled(String provider) { }

    };

    /** Report satellite status */
    private GnssStatus.Callback mStatusCallback = new GnssStatus.Callback() {
        @Override
        public void onStarted() {
            Log.i(TAG, "GNSS Callback Started");
        }

        @Override
        public void onStopped() {
            Log.i(TAG, "GNSS Callback Stopped");
        }

        @Override
        public void onFirstFix(int ttffMillis) {
            Log.i(TAG, "On First Fix???");
        }

        @Override
        public void onSatelliteStatusChanged(GnssStatus status) {
            Log.i(TAG, "GNSS Status: " + status.getSatelliteCount() + " satellites.");
        }
    };

private OnNmeaMessageListener mMessageListener = new OnNmeaMessageListener() {
    @Override
    public void onNmeaMessage(String message, long timestamp) {
        Log.v(TAG, "NMEA: " + message);
    }
};

@Override
protected void onDestroy() {
    super.onDestroy();

    if (mDevice != null) {
        try {
            mDevice.close();
            mDevice = null;
            mGpsDriver.unregister();
            mGpsDriver.close();
        } catch (IOException e) {
            Log.w(TAG, "Unable to close" + UART_DEVICE_NAME, e); }
    }
}

activity_main.xml

代码语言:javascript
复制
<TextView
    android:id="@+id/longbox"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="60dp"
    android:layout_marginTop="16dp"
    android:background="@android:drawable/editbox_background"
    android:visibility="visible"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/loctext" />

<Button
    android:id="@+id/gpsbtn"
    style="@android:style/Widget.Button"
    android:layout_width="267dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="32dp"
    android:text="@string/gpsbtn"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.501"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/altbox"
    app:layout_constraintVertical_bias="0.447" />

<TextView
    android:id="@+id/loctext"
    android:layout_width="66dp"
    android:layout_height="21dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:text="@string/loctext"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/citybox"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="16dp"
    android:background="@android:drawable/editbox_background"
    android:visibility="visible"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/latbox" />

<TextView
    android:id="@+id/lattext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="22dp"
    android:text="@string/lattext"
    app:layout_constraintEnd_toStartOf="@+id/latbox"
    app:layout_constraintTop_toBottomOf="@+id/loctext" />

<TextView
    android:id="@+id/longtext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="22dp"
    android:text="@string/longtext"
    app:layout_constraintEnd_toStartOf="@+id/longbox"
    app:layout_constraintTop_toBottomOf="@+id/loctext" />

<TextView
    android:id="@+id/citytext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="28dp"
    android:text="@string/citytext"
    app:layout_constraintEnd_toStartOf="@+id/citybox"
    app:layout_constraintTop_toBottomOf="@+id/lattext" />

<TextView
    android:id="@+id/countrytext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="28dp"
    android:text="@string/countrytext"
    app:layout_constraintEnd_toStartOf="@+id/countrybox"
    app:layout_constraintTop_toBottomOf="@+id/longtext" />

<TextView
    android:id="@+id/latbox"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="16dp"
    android:background="@android:drawable/editbox_background"
    android:visibility="visible"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/loctext" />

<TextView
    android:id="@+id/countrybox"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="60dp"
    android:layout_marginTop="16dp"
    android:background="@android:drawable/editbox_background"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/longbox" />

<TextView
    android:id="@+id/alttext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="30dp"
    android:text="@string/alttext"
    app:layout_constraintEnd_toStartOf="@+id/altbox"
    app:layout_constraintTop_toBottomOf="@+id/citytext" />

<TextView
    android:id="@+id/altbox"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="16dp"
    android:background="@android:drawable/editbox_background"
    android:visibility="visible"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/citybox" />

<TextView
    android:id="@+id/fixtext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="30dp"
    android:text="@string/fixtext"
    app:layout_constraintEnd_toStartOf="@+id/fixbox"
    app:layout_constraintTop_toBottomOf="@+id/countrytext" />

<TextView
    android:id="@+id/fixbox"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="60dp"
    android:layout_marginTop="16dp"
    android:background="@android:drawable/editbox_background"
    android:visibility="visible"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline"
    app:layout_constraintTop_toBottomOf="@+id/countrybox" />

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="302dp"
    android:orientation="vertical"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintGuide_percent="0.5"
    app:layout_constraintTop_toTopOf="parent" />
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-20 00:04:30

请在onLocationChanged()方法中使用以下内容:

代码语言:javascript
复制
TextView latbox = (TextView) findViewById(R.id.latbox);
latbox.setText("" + location.getLatitude());

TextView longbox = (TextView) findViewById(R.id.longbox);
longbox.setText("" + location.getLongitude());

TextView altbox = (TextView) findViewById(R.id.altbox);
altbox.setText("" + location.getAltitude());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50932499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档