【问题标题】:Android compass: GPS-basedAndroid 指南针:基于 GPS
【发布时间】:2013-09-12 12:38:19
【问题描述】:

我想创建一个 Android 指南针 - 仅基于 GPS。 我正在为其开发的设备没有任何加速度计或磁场传感器,这就是我必须仅依赖 android.location 类的原因。

到目前为止,访问 LocationManager 以显示当前 GPS 坐标对我来说工作正常(见下文)。我正在努力寻找最好的方法。我正在考虑电路角度计算 - 使用当前坐标和地理北坐标作为已知值来计算方向。

任何人都可以建议这种方法是否有意义甚至共享代码?

这是我目前得到的代码:

public class CompassActivity extends Activity implements LocationListener {


Compass myCompass;
private TextView mInfoText;
private LocationManager mLoc;

private static final Integer MINIMUM_UPDATE_INTERVAL = 5000; //update every 5s
private static final Integer MINIMUM_UPDATE_DISTANCE = 5; //update every 5m

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compass);
    myCompass = (Compass)findViewById(R.id.mycompass);

    mInfoText = (TextView) findViewById(R.id.infotext);
    mLoc = (LocationManager) getSystemService(LOCATION_SERVICE);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.compass, menu);
    return true;
}

@Override
protected void onResume() {
    mLoc.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_UPDATE_INTERVAL, MINIMUM_UPDATE_DISTANCE, this);
    super.onResume();

}

@Override
protected void onPause() {
    mLoc.removeUpdates(this);
    super.onPause();
}

@Override
protected void onStop() {
    finish();
    super.onStop();
}

@Override
public void onLocationChanged(Location loc) { //display coordinates
    System.out.println(loc.toString());
    String title = "Current location: ";
    final StringBuilder sb = new StringBuilder(title);
    sb.append("Longitude: ");
    sb.append(loc.getLongitude());
    sb.append("Latitude: ");
    sb.append(loc.getLatitude());
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            mInfoText.setText(sb.toString());
        }
    });
}

@Override
public void onProviderDisabled(String provider){ // message if GPS is disabled
    Toast.makeText(this, "GPS disabled", Toast.LENGTH_LONG).show();
    AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
    alertbox.setMessage("Please activate GPS!");
    alertbox.setNeutralButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface arg0, int arg1) {
            finish();
        }
    }
    );
    alertbox.show();
}


@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(this, "GPS enabled", Toast.LENGTH_LONG).show();
}


@Override
public void onStatusChanged(String provider, int status, Bundle b) { // called upon GPS status changes
    switch (status) {
    case LocationProvider.OUT_OF_SERVICE:
        Toast.makeText(this, "Status changed: out of service", Toast.LENGTH_LONG).show();
        break;
    case LocationProvider.TEMPORARILY_UNAVAILABLE:
        Toast.makeText(this, "Status changed: temporarily unavailable", Toast.LENGTH_LONG).show();
        break;
    case LocationProvider.AVAILABLE:
        Toast.makeText(this, "Status changed: available", Toast.LENGTH_LONG).show();
        break;
    }

}

}

【问题讨论】:

  • 为什么不在Location 上使用getBearing()
  • 您知道 GPS 无法在不移动的情况下获得方位,对吧?换句话说,您可以知道设备上次移动 的方向,但不能知道它现在面向的方向。因此,对于任何类型的零半径转弯都没有好处。对于汽车等,它会很好。
  • 是的,我知道这一点。我想除了继续前进之外没有其他解决方法......对吗?
  • 对。并非没有其他数据源(这就是大多数人使用加速/磁铁的原因)。对于一个四处走动的指南针,它可能做得不太好。
  • 谢谢,伙计们。我会看看这个。这似乎是定义。比从头开始编写计算代码更可行;)

标签: android gps digital-compass


【解决方案1】:

您现在根本无法向用户显示设备面向的方向。但如果您有可能获得 GPS 提供的设备位置,则可以显示设备移动的方向。

如果您获得位置更新,则需要两个位置来计算这两个位置之间的方位:

double x = Math.cos(lat2) * Math.sin(lng1-lng2);
double y = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(lng1-lng2);
double bearing = Math.atan2(x, y);

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    相关资源
    最近更新 更多