2012-03-14 95 views
0

我試圖通過GPS獲取當前用戶位置。我已經閱讀了關於這個的谷歌文檔,並嘗試了一些教程,但似乎沒有任何工作正常。這裏是我的代碼: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 

} 

我知道,GPS往往不是在室內工作,所以我就去站在外面5分鐘,並沒有發揮作用。當我使用模擬器來模擬一個位置時,它返回的位置很長 - 沒有錯誤。

而且 - 我已經包括在清單文件所需的權限如下:

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

任何想法的,爲什麼這是不工作?

+1

您是否使用過谷歌地圖或一些GPS測試工具,你真的從GPS獲得位置?在某些設備上可能需要很長時間。你甚至可能禁用GPS。 – zapl 2012-03-14 14:23:05

+0

定義真的很長 - 我一直在盯着設備15分鐘,除了屏幕頂部的衛星圖像以外,沒有做任何動畫 - 你知道整個衛星發送信號的東西... – Katana24 2012-03-14 14:25:36

+1

GPS沒有直到GPS圖標停止閃爍。取決於您的位置/衛星可視性/援助數據的可用性等長= 30分鐘左右。 – zapl 2012-03-14 14:30:01

回答

0

好吧,我解決了這個問題 - 我決定調整代碼來創建監聽器,而不是在類中實現它。我還確保我在requestLocationUpdates上同時調用了NETWORK_PROVIDER和GPS_PROVIDER以涵蓋兩個基地。下面是修改後的代碼,請注意,註釋掉的地理編碼部分崩潰的時刻節目,我還沒有制定出臭蟲尚未...

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()方法上的兩個requestLocationUpdate方法。 onResume()方法可能會更改爲獲取最後一個已知位置(如果有),而不是重新建立位置更新方法。

希望這可以幫助其他人...