2011-12-07 35 views
3

我用獲取當前位置驗證碼:我怎樣才能在Android當前的位置?

LocationManager lm = (LocationManager) this 
     .getSystemService(Context.LOCATION_SERVICE); 
List<String> providers = lm.getProviders(true); 

/* 
* Loop over the array backwards, and if you get an accurate location, 
* then break out the loop 
*/ 
Location l = null; 

for (int i = providers.size() - 1; i >= 0; i--) { 

    l = lm.getLastKnownLocation(providers.get(i)); 
    if (l != null) 
     break; 
} 

if (l==null) { 
    return; 
} 
Toast.makeText(this, String.valueOf(l.getLatitude()), Toast.LENGTH_LONG).show(); 

但我從來沒有看到一個緯度,因爲l爲null始終。我的Manifest文件包含ACCESS_FINE_LOCATION和INTERNET權限。我犯了什麼錯誤?

+1

請參閱http://developer.android.com/guide/topics/location/obtaining-user-location.html。您需要添加位置偵聽器並請求更新。 – blessenm

+1

爲什麼你使用這段代碼?如果不necesory使用它然後使用此:http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android和這裏是指開發者網站:HTTP://developer.android.com/reference/android/location/LocationManager.html –

+0

這可能會爲你工作,我猜: http://stackoverflow.com/a/3145655/265167 –

回答

0

我認爲最好的開源項目實施地點是在谷歌代碼四方的 http://code.google.com/p/foursquared/

相關的代碼是在com.joelapenna.foursquared.loaction.BestLoactionListener

您可以檢查出他們如何處理Loaction,以及如何更新當前的Loaction,這對我有很大的幫助。

0

下面的代碼爲我工作。我也使用GPS以及網絡服務來確定設備的當前位置。

private void findCurrentLocation() 
{ 
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    if (locationManager == null) 
    { 
     Toast.makeText(CityActivity.this, "Location Manager Not Available", 
     Toast.LENGTH_SHORT).show(); 
     return; 
    } 
    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    if (location == null) 
     location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
    if (location != null) 
    { 
     lat = location.getLatitude(); 
     lng = location.getLongitude(); 
     myFunction(location); 
    } 
    else 
    { 
     locationListener = new LocationListener() 
     { 
      @Override 
      public void onStatusChanged(String arg0, int arg1, Bundle arg2) 
      {;} 

      @Override 
      public void onProviderEnabled(String arg0) 
      {;} 

      @Override 
      public void onProviderDisabled(String arg0) 
      {;} 

      @Override 
      public void onLocationChanged(Location l) 
      { 
       location = l; 
       locationManager.removeUpdates(this); 
       if (l.getLatitude() == 0 || l.getLongitude() == 0) 
       {;} 
       else 
       { 
        lat = l.getLatitude(); 
        lng = l.getLongitude(); 
        myFunction(location); 
       } 
      } 
     } 
     if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, f, locationListener); 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 10f, locationListener); 
     locationtimer = new CountDownTimer(30000, 5000) 
     { 
      @Override 
      public void onTick(long millisUntilFinished) 
      { 
       if (location != null) 
        locationtimer.cancel(); 
      } 

      @Override 
      public void onFinish() 
      { 
       if (location == null) 
       {;} 
      } 
     }; 
     locationtimer.start(); 
    } 
} 
+0

代碼在哪裏? – eeerahul

+0

錯誤對不起,我複製某處else.Now其正確粘貼代碼 –

相關問題