【问题标题】:Android GPS data is not displayedAndroid GPS 数据不显示
【发布时间】:2016-05-18 15:52:58
【问题描述】:

我是安卓开发新手。在过去的几个月里,我构建了一个现在可以运行的应用程序。现在我想改进每次打开应用时困扰我的所有小问题。

在这种情况下,我尝试获取 GPS(仅 GPS)位置。每当位置更新进入时,它应该被显示。

出于任何原因,第一个位置(我可以在调试日志中看到它们)被忽略并且不会显示在我的设备上。

代码如下:

public class GpsLogging extends Activity{
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gps_logging);

    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}

public class MyLocationListener implements LocationListener
{
    @Override
    public void onLocationChanged(Location loc)
    {
        double lat = loc.getLatitude();
        double lon = loc.getLongitude();
        float accuracy = loc.getAccuracy();

        TextView textView = (TextView) findViewById(R.id.gpsPosition);
        textView.setText("My current location is: \n" +
                "Latitude = " + lat +"\n"+
                "Longitude = " + lon +"\n"+
                "Accuracy = " + accuracy +" m");

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

如果有人知道为什么会忽略不精确的数据,那就太好了。

这里是请求的对应xml:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.example.GpsLogging">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/gpsPosition"
    android:text="Aquiring GPS Position"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="46dp" />

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progressBar"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

【问题讨论】:

  • 发布布局 xml 也...
  • 您的设备已激活 GPS?检查
  • “我可以在调试日志中看到它们”:- 您在其中为日志编写了打印语句。

标签: android gps


【解决方案1】:

使用

 .getLastKnownLocation()

方法。

示例:

Location earlierLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

原因:您只需在 onLocationChanged(新 gps)上打印。

【讨论】:

  • 谢谢。我认为传入的数据总是一个更新的位置。 inline '来源:2 纬度:53.072959 经度:8.793933 高度:181.399994 速度:0.000000 方位:0.000000 精度:7.000000 时间戳:1446805781000 rawDataSize:0 rawData:0x0 会话状态:0 技术掩码:1'
  • @Entropia 很好。那你就可以接受我的回答了。干杯
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多