- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想创建一个 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;
}
}
最佳答案
您根本无法向用户显示设备此时所面对的方向。但是,如果您有可能获得 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);
希望这对您有所帮助!
关于安卓罗盘 : GPS-based,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18764505/
我是 objective-c 的新手,所以如果我的问题太垃圾,我想道歉。 我正在编写一个指南针,但它正在运行(感谢教程)。给出了实际位置(经度和纬度)。我的指南针内的箭头是否可以向我显示特定位置(经度
我正在为移动设备开发“指南针”。我有以下几点: point 1 (current location): Latitude = 47.2246, Longitude = 8.8257 point 2 (
有没有办法在 SQL Server 2008R2 中知道一个点是否在另一点的南、东等? 例如,我有一个来源 point(lat1,lng1)我想知道在哪里point(lat2,lng2)位于该原点:北
我正在尝试在 Windows Phone 项目上使用 GART ( http://gart.codeplex.com/ ),它需要使用陀螺仪。 大多数时候我觉得陀螺仪没有校准并且显示错误的结果。 有没
在我的印象中,iOS 中 Google map 的指南针效果非常好。我说的是 map 中蓝色小“我的位置”点的罗盘箭头,请参阅 Google Maps SDK - My Location .在最近的1
我是一名优秀的程序员,十分优秀!