2011-09-23 22 views
0

私は自分のGPS位置を取得しようとしており、アクティビティの上部にあるテキストビュー(tv2)で更新しています。私のアクティビティはカメラビューを表示し、GPS座標をキャプチャし、テキストビューに書き込むことができます。また、コンパスの&加速度データも取得できますが、今は問題ではありません。なぜGPS位置を取得できないのですか?

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

実際には、デバイスは時々GPSをトラッキングするのに時間がかかります。LocationManager.GPS_PROVIDERをLocationManager.NETWORK_PROVIDERに交換してください。 –

+0

それは動作しませんでした。また、私は外部のアプリケーション(ないカメラ/ ARアプリ)でそれが完璧に動作し、私のコードをテストする場合は、問題が他のものでなければならないことを言った – NullPointerException

答えて

0

が、私はそれが携帯電話のバグのいくつかの種類だと思うが、それは、加速度計、コンパスと正常に動作することはできませんGPSは同時に使用されています

関連する問題