血莲丹 2019-04-22 17:03 采纳率: 25%
浏览 2386

使用Android studio实现简单的GPS定位,为什么真机上不显示定位信息?

布局界面
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
android:textColor="#ff00ff"
android:textSize="40sp"
android:id="@+id/tv0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您的定位信息:"
/>
android:textColor="#ff00ff"
android:textSize="40sp"
android:id="@+id/tv1"
android:text="xinxi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

MainActivity.java界面:
package com.example.gps3;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView textView;
LocationManager manager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.tv1);
    manager = (LocationManager) getSystemService(LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }

    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 8, new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            updateLocation(location);
        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    });
}
public void updateLocation(Location location){
    textView.setText("");
    textView.append("经度是:"+location.getLongitude());
    textView.append("\n纬度是:"+location.getLatitude());
    textView.append("\n高度:"+location.getAltitude());
    textView.append("\n方向:"+location.getBearing());
    textView.append("\n速度:"+location.getSpeed());
}

}

为什么我的第二个TextView不显示定位信息?
谢谢,第一次提问,排版不好,还请见谅!

  • 写回答

2条回答 默认 最新

  • qq_36298675 2019-04-22 17:27
    关注
     // 获取所有可用的位置提供器
            List<String> providerList = locationManager.getProviders(true);
    

    再根据集合返回的内容判断当前手机可用的提供器 有WiFi,GPS这些,如果这些都没有手机是没法定位的https://blog.csdn.net/jiuyuefenglove/article/details/80082076

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100