【问题标题】:How to get location Lat and Long just from GPS not google api in android?如何仅从 GPS 而不是 android 中的 google api 获取位置 Lat 和 Long?
【发布时间】:2014-07-16 15:16:46
【问题描述】:

我开发了一个应用程序,它在使用 Google API 进行定位时运行良好,但是当我仅在智能手机中选择 GPS 时,它显示没有位置。我下面的代码有什么问题:

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

    Button button=(Button)findViewById(R.id.button1);
    button.setOnClickListener(theButtonListener);

    locationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
    boolean enabled = locationManager
              .isProviderEnabled(LocationManager.GPS_PROVIDER);
    if(enabled)
    {
        //Log.d(TAG,"YES enabled");
        Toast.makeText(MainActivity.this, "Enabled", 2000).show();
    }
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Toast.makeText(MainActivity.this, provider.toString(), 5000).show();
    Location location = locationManager.getLastKnownLocation(provider);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    //Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null) {
          //Log.d(TAG, location.toString());
        Toast.makeText(MainActivity.this, location.toString(), 10000).show();
        onLocationChanged(location); //<6>
        }
    else
        Toast.makeText(MainActivity.this, "No location", 5000).show();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    //locationManager.requestLocationUpdates(provider, 400, 1, this);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 400, 1, this);
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    Log.d(TAG, "onLocationChanged with location " + location.toString());
    String text = String.format("Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f",    location.getLatitude(), 
                  location.getLongitude(), location.getAltitude(), location.getBearing());
    Toast.makeText(MainActivity.this, text, 10000).show();
}

@Override
public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}

@Override
public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub
     Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();
}


@Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
 //         locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
        Location location =   locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Toast.makeText(MainActivity.this, provider.toString(), 5000).show();
        //Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
              //Log.d(TAG, location.toString());
            Toast.makeText(MainActivity.this, location.toString(), 10000).show();
            onLocationChanged(location); //<6>
            }
        else
            Toast.makeText(MainActivity.this, "No location", 5000).show();

    }
};

顺便问一下,google API 如何在内部工作,它如何找到我的 LAT 和 LON?

【问题讨论】:

  • getLastKnownLocation 可以返回 null
  • 如果没有来自 GPS 的信号(或从来没有),那么该位置可以为空。你在外面吗,你在等待 GPS 定位,它甚至来了吗?可能需要一段时间..

标签: android gps location


【解决方案1】:

onLocationChanged 是一个回调,意味着它在位置发生变化时被系统调用。它是事件驱动的,你不应该明确地调用它。你必须等到 GPS 得到修复,你不能立即得到一个。当应用程序第一次运行时,可能没有“最后一个位置”,因此它将返回 null。

【讨论】:

    猜你喜欢
    • 2012-03-20
    • 2015-05-20
    • 2012-09-22
    • 1970-01-01
    • 2020-10-28
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    相关资源
    最近更新 更多