Android:不间断位置计算
我目前正在做一个机器人项目,我决定通过 Android 手机(三星 Galaxy S)来驱动我的机器人。我的问题是我找不到“每次”计算GPS位置的方法,我的意思是,驾驶需要非常小的刷新时间,而我无法得到它。起初,我以为我只需要输入这样的内容:
mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, this.refreshTime, 0, this);
刷新时间是一个变量即时,其值为 500,例如,但它不起作用,因为我注意到这个值只是一个提示,而不是真正的参数。所以我迷路了^^
有什么办法可以做到这一点吗?
我在这个主题中受益,问你一个问题...你们知道为什么当我设置 GPS_PROVIDER 时我的位置没有被计算吗?我的卫星已打开,但没有返回任何内容,所以我想我做错了...
这是我的完整代码:
package com.pIndus.gps;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class Loc extends Activity {
/** Called when the activity is first created. */
private long refreshTime = 1000;
private LocationManager mLocManager;
private LocationListener mLocListener;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocListener = new MyLocationListener();
mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, this.refreshTime, 0, mLocListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps désactivé", Toast.LENGTH_SHORT ).show();
Log.i("GPS", "GPS Desactivé");
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps activé", Toast.LENGTH_SHORT).show();
Log.i("GPS", "GPS Activé");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText( getApplicationContext(),"Statut changé", Toast.LENGTH_SHORT).show();
Log.i("GPS", "Statut changé");
}
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "Localisation courante:\n " + "Latitude : " + loc.getLatitude() + "\n" + "Longitude : " + loc.getLongitude();
Log.i("GPS", "Localisation calculée");
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
}
public void onPause()
{
mLocManager.removeUpdates(mLocListener);
super.onPause();
}
public void onStop()
{
mLocManager.removeUpdates(mLocListener);
super.onStop();
}
}
希望你们理解我,我知道我的英语很糟糕。
非常感谢您的帮助
I'm currently doing a project of robotics and I decided to drive my robots thanks to an Android Phone (Samsung Galaxy S). My problem is that I can't find the way to calculate the GPS location "everytime", I mean, the driving requires a very tiny refresh-time, and I can't get it. At first, I thought I just had to put something like this:
mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, this.refreshTime, 0, this);
With refreshTime being a variable instant which values is 500 e.g., but it doesn't work, as I noticed this value is just a hint, not really a parameter. So i'm lost ^^
Is there any way to do that ?
I profit in this topic to ask you a question... Do you know guys why my location is not being calculated when I set GPS_PROVIDER ? My satellites are turned on but nothing got returned so I guess I'm doing wrong...
Here's my complete code:
package com.pIndus.gps;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class Loc extends Activity {
/** Called when the activity is first created. */
private long refreshTime = 1000;
private LocationManager mLocManager;
private LocationListener mLocListener;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocListener = new MyLocationListener();
mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, this.refreshTime, 0, mLocListener);
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener
{
@Override
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps désactivé", Toast.LENGTH_SHORT ).show();
Log.i("GPS", "GPS Desactivé");
}
@Override
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps activé", Toast.LENGTH_SHORT).show();
Log.i("GPS", "GPS Activé");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText( getApplicationContext(),"Statut changé", Toast.LENGTH_SHORT).show();
Log.i("GPS", "Statut changé");
}
@Override
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "Localisation courante:\n " + "Latitude : " + loc.getLatitude() + "\n" + "Longitude : " + loc.getLongitude();
Log.i("GPS", "Localisation calculée");
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
}
public void onPause()
{
mLocManager.removeUpdates(mLocListener);
super.onPause();
}
public void onStop()
{
mLocManager.removeUpdates(mLocListener);
super.onStop();
}
}
Hope you guys understood me, I know my english is very bad.
Thank you very much for the help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为你可以获得比 1Hz 更高的频率... GPS 接收器芯片通常会定期在串行线路上(逻辑上)发送测量结果,因此无法告诉它“我现在想要一个位置” 。这个定期间隔也是可调的,但操作系统似乎不太可能愿意将其调整为比 1Hz 小得多的值,仅仅因为某些应用程序要求。
然而,由于 GPS 定位无论如何都不是那么准确,所以这似乎并不是一个大问题。 (当然,除非您正在建造一架半秒内飞行 200 米的喷气式机器人飞机:))
我建议使用卡尔曼滤波器或类似的方法根据之前的修复和速度来推断位置。 (如果您添加来自其他传感器、电机控制等的更多信息,此估计值可能比您通过每秒测量 10 次 GPS 位置获得的估计值要好得多。)
I don't think you can get much greater frequency than 1Hz... GPS receiver chips usually send the measurements (logically) on a serial line at regular intervals, so there is no way of telling it "I want a location, NOW". This regular interval is adjustable, too, but it seems unlikely that the OS is willing to adjust it to a much smaller value than 1Hz, just because some app requested.
However, since GPS fixes aren't that accurate anyway, this doesn't seem to be a problem that big. (Unless you're building a jet-powered robot aircraft that flies 200 meters in half a second, of course :))
I would suggest using a Kalman filter or a similar method to extrapolate the position based on the previous fixes and speed. (If you add some more info from other sensors, motor control etc, this estimate may be much better than the one you could get by measuring GPS position 10 times a second.)