2011-12-04 10 views
3

自分の携帯電話の現在位置を自分のプログラムでGPS経由で取得しようとしています。AndroidプログラムでGPS座標が不正確です

問題は、座標が不正確(度のみ)または場所が指定されていないことです。私は3つ目のプログラムを今度はそれぞれ別々のチュートリアルでプログラムしましたが、うまくいきませんでした。

はそれが答えではないので、設定された許可を使用しています。だからここ)

を私のコードは次のとおりです。答えるため

public class GpsActivity extends Activity{ 

private TextView latituteField; 
private TextView longitudeField; 
private TextView dateField; 
private TextView timeField; 
private LocationManager locationManager; 
private String provider; 
private LocationListener listener; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    latituteField = (TextView) findViewById(R.id.TextView02); 
    longitudeField = (TextView) findViewById(R.id.TextView04); 
    dateField = (TextView) findViewById(R.id.tvdate); 
    timeField = (TextView) findViewById(R.id.tvtime); 
    GregorianCalendar g = new GregorianCalendar(); 
    g.setTimeInMillis(System.currentTimeMillis()); 

    DateFormat df = DateFormat.getInstance(); 
    Date d = df.getCalendar().getTime(); 
    dateField.setText(""+d.getDate()+"."+(d.getMonth()+1)+"."+(1980+d.getYear())); 
    timeField.setText(""+g.getTime().getHours()+"h "+g.getTime().getMinutes()+"m "+g.getTime().getSeconds()+"s"); 


    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    // Define the criteria how to select the locatioin provider -> use 
    // default 
    Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 

    provider = locationManager.getBestProvider(criteria, false); 
    Location location = locationManager.getLastKnownLocation(provider); 

    // Initialize the location fields 
    if (location != null) { 
     System.out.println("Provider " + provider + " has been selected."); 
     int lat = (int) (location.getLatitude()); 
     int lng = (int) (location.getLongitude()); 

     latituteField.setText(""+lat+"//"+location.getAccuracy()); 
     longitudeField.setText(""+lng+"//"+location.getProvider()); 
    } else { 
     latituteField.setText("Provider not available"); 
     longitudeField.setText("Provider not available"); 
    } 
    listener = new MyLocationListener(); 
    locationManager.requestLocationUpdates("gps" ,10000L, 10.0f, listener); 

} 

class MyLocationListener implements LocationListener{ 
    public void onLocationChanged(Location location) { 
      // TODO Auto-generated method stub 
      if (location != null){ 
        latituteField.setText(("Lat: " + location.getLatitude())); 
        longitudeField.setText("LLong: " + location.getLongitude()); 
      } 
    } 
    public void onProviderDisabled(String provider) { 

    } 
    public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 
    } 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 
    } 
} 
} 

ありがとう!

ルネ

答えて

0

あなたはたぶん弱い信号を得るでしょう。おおよその位置を取得するためにアンドロイドロケーションAPIを使用することができます。

「良い」信号を得るためには、屋外に立って空を自由に見てください。あなたがそれをしても、正確な信号を得ることはありません。電話機のGPSは、省エネルギーのために最適化されており、正確さのためではありません。彼らはあなたの携帯電話にあまりにも高価なチップを置くことはありません...

+0

さらにUdoの答えには、あなたが場所の修正の正確さを照会できることを忘れないでください...ここでの精度に関する議論:http:// stackoverflow.com/questions/3052517/how-is-location-accuracy-measured-in-android – Basic

関連する問題