2012-03-14 12 views
0

現在、GPS経由で現在のユーザの位置を取得しようとしています。私はこれについてのGoogleの文書を読んでいくつかのチュートリアルを試みたが、何も正しく動作するようだ。ここに私のコードです:GPSロケータが動作しない

public class useGPS extends Activity implements LocationListener 
{ 
private static final String TAG = "useGPS:"; 
LocationManager myLocManager; 
TextView locationData; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.use_gps); 

    myLocManager = (LocationManager)this.getSystemService(LOCATION_SERVICE); 
    Location location = myLocManager.getLastKnownLocation(myLocManager.GPS_PROVIDER) ; 
    if(location != null) 
    { 
     Log.d(TAG,location.toString()); 
     Toast.makeText(this, "Location changed...", Toast.LENGTH_SHORT); 
     this.onLocationChanged(location); 
    } 
} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    myLocManager.requestLocationUpdates(myLocManager.GPS_PROVIDER, 3000, 0, this); 

} 

@Override 
protected void onPause() 
{ 
    super.onPause(); 
    myLocManager.removeUpdates(this); 
} 


@Override 
public void onLocationChanged(Location location) 
{ 
    Log.d(TAG,"onLocationChanged with location: "+location.toString()); 

    // Return a string representation of the latitude & longitude. Pass it to the textview and update the text 
    String text = "Lat: "+location.getLatitude()+"\nLong: "+location.getLongitude(); 

    locationData = (TextView)findViewById(R.id.locationData); 
    locationData.setText(text); 
} 

@Override 
public void onProviderDisabled(String provider) { 
    Toast.makeText(getApplicationContext(), "Provider Disabled...", Toast.LENGTH_SHORT); 

} 

@Override 
public void onProviderEnabled(String provider) 
    { 
    Toast.makeText(getApplicationContext(), "Provider Enabled...", Toast.LENGTH_SHORT); 

} 

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

} 

私が行って、5分のために外に立っていたし、それは違いはありませんでしたので、GPSは屋内で動作しない傾向があることを承知しています。エミュレータを使用して位置をシミュレートすると、エラーのない位置の緯度が&長くなりました。

も ​​- 次のように私は、マニフェストファイルで必要な権限が含まれている:

<uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION"></uses-permission> 

すべてのアイデアは、これが動作しない理由としてですか?

+1

googleマップやいくつかのGPSテストツールは、実際にGPSからの場所を取得する?いくつかのデバイスではかなり時間がかかります。 GPSが無効になっている可能性もあります。 – zapl

+0

本当に長く定義する - 私は15分間デバイスを凝視していて、画面上部の衛星画像以外に何もしていない - 衛星送信信号のことを知っている... – Katana24

+1

GPSはないGPSアイコンが点滅を停止するまでの位置。あなたの場所/衛星の可視性/支援データの可用性などに応じて、長い= 30分程度です。 – zapl

答えて

0

私は問題を解決しました。クラスに実装する代わりにリスナーを作成するコードを調整することにしました。私は、両方の基盤をカバーするために、requestLocationUpdates上でNETWORK_PROVIDERとGPS_PROVIDERの両方を呼び出すようにしました。ここで改訂されたコードは、コメントアウトジオコーディング部分は一瞬のためのプログラムをクラッシュすることに注意してください、私は1つの最終ポイントをバグまだ...

public class useGPS extends Activity 
{ 
private static final String TAG = "useGPS:"; 

TextView locationData, addressData; 
LocationManager myLocManager; 
LocationListener locationListener; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.use_gps); 

    // Acquire a reference to the system Location Manager 
    myLocManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

    // Define a listener that responds to location updates 
    locationListener = new LocationListener() 
    { 

     @Override 
     public void onLocationChanged(Location location) 
     { 
      // Return a string representation of the latitude & longitude. Pass it to the textview and update the text 
      String text = "Lat: "+location.getLatitude()+"\nLong: "+location.getLongitude(); 

      locationData = (TextView)findViewById(R.id.locationData); 
      locationData.setText(text); 
      /** 
      try 
      { 
       // Reverse Geocoding 
       Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault()); 
       List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(),0); 

       if(addresses != null) 
       { 
        // Get return address data 
        Address returnedAddress = addresses.get(0); 
        StringBuilder strReturnedAddress = new StringBuilder("Address:\n"); 

        for(int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) 
        { 
         // Return the address 
         strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); 
        } 
        // Set our textview to the address returned 
        addressData.setText(strReturnedAddress.toString()); 
       } else { 
        addressData.setText("Sorry, no address returned..."); 
       } 

      } catch(IOException e) 
      { 
       // Catch the Exception 
       e.printStackTrace(); 
       addressData.setText("Error: Cannot get Address!"); 
      } 
      */ 
     } 

     @Override 
     public void onProviderDisabled(String provider) 
     { 

     } 

     @Override 
     public void onProviderEnabled(String provider) 
     { 

     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) 
     { 

     } 

    }; 

    // The two lines below request location updates from both GPS & the network location provider 
    myLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,locationListener); 
    myLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener); 
} 

@Override 
protected void onResume() 
{ 
    super.onResume(); 
    // The two lines below request location updates from both GPS & the network location provider 
    myLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,locationListener); 
    myLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener); 
} 

@Override 
protected void onPause() 
{ 
    super.onPause(); 
    // The two lines below request location updates from both GPS & the network location provider 
    myLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,locationListener); 
    myLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener); 
} 

} 

を働いていないされて - 私はわからないんだけどonResume()メソッドとonPause()メソッドで2つのrequestLocationUpdateメソッドが必要かどうかをこの瞬間のうちに確認してください。 onResume()メソッドは、場所の更新メソッドを再設定する代わりに、おそらく最後の既知の場所がある場合は取得するように変更されます。

これは他の人に役立つことを望みます。