安卓指南针:基于GPS



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

到目前为止,访问位置管理器以显示当前的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;
    }
}

}

您根本无法向用户显示设备当前面向的方向。但是,如果您有可能获得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);

希望这有帮助!

最新更新