我正在尝试获取我的GPS位置,并使用textview(tv2)更新它位于活动的顶部。我的活动显示了摄像机视图并捕获GPS共存,并且必须在textview上写入它们。它也得到了指南针&加速度计数据,但现在不是问题。
有些事情是错误的,因为我无法捕获GPS数据...但我不明白什么是错的。如果我从头开始创建一个新项目并粘贴GPS位置请求代码,它就会得到正确的代码,但是在这个应用程序中,我无法获得GPS位置......使用相同的代码。出了什么问题?
对于cameraView我正在使用本指南中编写的默认cameraView代码:http://www.devx.com/wireless/article/42482/1954
这是我的主要课程:
public class AugmentedRealitySampleActivity extends Activity implements Runnable{
private CustomCameraView cv=null;
private TextView tv1;
private TextView tv2;
private TextView tv3;
private TextView tv4;
private TextView tv5;
private TextView tv6;
public static SensorManager sensorMan;
//variables para obtener mi posicion:
LocationManager mLocationManager;
Location mLocation;
MyLocationListener mLocationListener;
Location currentLocation = null;
private float direction; //compass
//private Location curLocation; //gps
public volatile float inclination; //accelerometer
double lat=-1;
double lon=-1;
public void onCreate(Bundle savedInstanceState)
{
//try{
super.onCreate(savedInstanceState);
cv = new CustomCameraView(this.getApplicationContext());
FrameLayout rl = new FrameLayout(this.getApplicationContext());
LinearLayout ll= new LinearLayout(this.getApplicationContext());
ll.setOrientation(LinearLayout.VERTICAL);
setContentView(rl);
rl.addView(cv);
rl.addView(ll);
//} catch(Exception e){}
tv1=new TextView(getApplicationContext());
tv2=new TextView(getApplicationContext());
tv3=new TextView(getApplicationContext());
tv4=new TextView(getApplicationContext());
tv5=new TextView(getApplicationContext());
tv6=new TextView(getApplicationContext());
tv1.setBackgroundColor(Color.BLACK);
tv2.setBackgroundColor(Color.BLACK);
//tv3.setBackgroundColor(Color.BLACK);
//tv4.setBackgroundColor(Color.BLACK);
//tv5.setBackgroundColor(Color.BLACK);
//tv6.setBackgroundColor(Color.BLACK);
ll.addView(tv1);
ll.addView(tv2);
ll.addView(tv3);
ll.addView(tv4);
ll.addView(tv5);
ll.addView(tv6);
sensorMan = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensorMan.registerListener(Compasslistener, sensorMan.getDefaultSensor(SensorManager.SENSOR_ORIENTATION), SensorManager.SENSOR_DELAY_FASTEST);
sensorMan.registerListener(Accelerometerlistener, sensorMan.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST);
//LocationManager locMan;
//locMan = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
//locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0, gpsListener);
//getLatitude(); getLongitude(); bearingTo(); distanceTo();
tv1.setText("Test1");
tv2.setText("Test2");
tv3.setText("Test3");
tv4.setText("Test4");
tv5.setText("Test5");
tv6.setText("Test6");
Thread thread = new Thread(this);
thread.start();
}
////////////////////////////////////////////////
//COMPASS:
////////////////////////////////////////////////
SensorEventListener Compasslistener = new SensorEventListener()
{
public void onAccuracyChanged(Sensor arg0, int accuracy){ }
public void onSensorChanged(SensorEvent evt)
{
float vals[] = evt.values;
direction = vals[0];
tv1.setText("Direction= " + direction);
}
};
////////////////////////////////////////////////
//ACCELEROMETER:
////////////////////////////////////////////////
private SensorEventListener Accelerometerlistener = new SensorEventListener()
{
public volatile float direction = (float) 0;
//public volatile float rollingZ = (float)0;
public volatile float kFilteringFactor = (float)0.05;
public float aboveOrBelow = (float)0;
public void onAccuracyChanged(Sensor arg0, int arg1){}
public void onSensorChanged(SensorEvent evt)
{
float vals[] = evt.values;
if(evt.sensor.getType() == Sensor.TYPE_ORIENTATION)
{
float rawDirection = vals[0];
direction =(float) ((rawDirection * kFilteringFactor) + (direction * (1.0 - kFilteringFactor)));
inclination = (float) ((vals[2] * kFilteringFactor) + (inclination * (1.0 - kFilteringFactor)));
if(aboveOrBelow > 0)
inclination = inclination * -1;
if(evt.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
aboveOrBelow = (float) ((vals[2] * kFilteringFactor) + (aboveOrBelow * (1.0 - kFilteringFactor)));
tv3.setText("Inclination= " + inclination);
tv4.setText("Direction2= " + direction);
tv5.setText("kFilteringFactor= " + kFilteringFactor);
tv6.setText("aboveOrBelow= " + aboveOrBelow);
}
}
};
////////////////////////////////////////////////////////////////////////
//Métodos del Hilo que obtiene la posicion GPS del usuario periodicamente.
///////////////////////////////////////////////////////////////////////
public void run() {
mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
Looper.prepare();
mLocationListener = new MyLocationListener();
try{
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 200, 0, mLocationListener);
}catch(Exception e){}
//try {
// wait(100);
//}catch (InterruptedException e) {}
Looper.loop();
}
}
private class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc) {
if (loc != null) {
try{
currentLocation = loc;
handler.sendEmptyMessage(0);
}catch(Exception e){}
}
}
public void onProviderDisabled(String provider) { }
public void onProviderEnabled(String provider) { }
public void onStatusChanged(String provider, int status, Bundle extras) { }
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (currentLocation!=null)
{
lat=currentLocation.getLatitude();
lon=currentLocation.getLongitude();
if (lat==-1 && lon ==-1) /// si no existe ninguna posicion GPS anterior en el telefono, no hago nada
{
}
else //// si existe alguna posicion anterior (es decir, si el GPS del telefono ha sido activado al menos alguna vez en su vida util)
{
tv2.setText("Location= "+lat+" "+lon);
}
}
}
};
}
答案 0 :(得分:0)
您是否已获得AndroidManifest的许可?
像:
<android.permission.ACCESS_COARSE_LOCATION" />
<android.permission.ACCESS_FINE_LOCATION" />
或在android开发者文档中查看更多权限: http://developer.android.com/reference/android/Manifest.permission.html
答案 1 :(得分:0)
我认为这是手机的某种错误,加速度计,指南针和GPS同时使用时无法正常工作