2013-03-27 75 views
6

什麼是獲取當前位置Android中的以下情形的最好辦法,Android的最好的方式來獲得當前位置

  1. 如果GPS不可用,從網絡提供商處獲得位置

  2. 如果GPS可用並且可以獲得當前位置,則從GPS提供商獲取位置

  3. 如果GPS可用但無法獲得當前位置(即不斷搜索位置),則從網絡提供商處獲得位置。

    現在我可以從網絡獲取位置,如果GPS不可用,滿足上述情況的最佳答案是高度讚賞。提前致謝。

+3

看看這個庫:https://code.google.com/p/little-fluffy-location-library/ – dannyroa 2013-03-27 07:08:24

+0

您是否檢查舊帖子 1. http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android 2. http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-用戶當前所在位置的健壯路徑/ 3145655#3145655 – surhidamatya 2013-03-27 07:29:14

+0

最簡單的方法是使用像這樣的更高級別的庫:https://github.com/delight-im/Android -SimpleLocation – caw 2015-03-04 22:55:01

回答

0
If GPS is available and can get current location, 

對於上面的問題,你可以嘗試這樣的..

使用這個你可以得到當前位置的經度和緯度,然後通過獲得地圖的價值。

public class MyLocationListener implements LocationListener 

{ 

@Override 

public void onLocationChanged(Location loc) 

{ 

loc.getLatitude(); 

loc.getLongitude(); 

String Text = 「My current location is: 「 + 

「Latitud = 「 + loc.getLatitude() + 

「Longitud = 「 + loc.getLongitude(); 

Toast.makeText(getApplicationContext(), 

Text, 

Toast.LENGTH_SHORT).show(); 

} 

@Override 

public void onProviderDisabled(String provider) 

{ 

Toast.makeText(getApplicationContext(), 

「Gps Disabled」, 

Toast.LENGTH_SHORT).show(); 

} 

@Override 

public void onProviderEnabled(String provider) 

{ 

Toast.makeText(getApplicationContext(), 

「Gps Enabled」, 

Toast.LENGTH_SHORT).show(); 

} 

@Override 

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

{ 

} 

} 

} 
0

class member boolean mIsGpsFix;

請求GPS位置更新和建立一個倒數計時器

mCountDown.start(); 

private CountDownTimer mCountDown = new CountDownTimer(time to wait for Gps fix, same as right) 
{ 

    @Override 
    public void onTick(long millisUntilFinished) 
    { 

    } 

    @Override 
    public void onFinish() 
    { 
     // No fix after the desire amount of time collapse 
     if (!mIsGpsFix) 
     // Register for Network 
    } 
}; 
2

好了,你可以使用TimerTimerTask類。

LocationManager manager; 
TimerTask mTimertask; 
GPSLocationListener mGPSLocationListener; 
int i = 0; //Here i works as counter; 
private static final int MAX_ATTEMPTS = 250; 

public void getCurrentLocation() { 
    manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    mGPSLocationListener = new GPSLocationListener(); 

    manager.addGpsStatusListener(mGPSStatusListener); 
    mTimerTask = new LocTimerTask(LocationManager.GPS_PROVIDER); 

    if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
     Log.v(TAG, "GPS ENABLED"); 
     manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 
       50.0f, mGPSLocationListener); 
    } else { 
     turnGPSOn(); 
     manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 
       50.0f, mGPSLocationListener); 
    } 

    if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
     manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000L, 
       50.0f, mNetworkLocationListener); 
    } 

    if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
     Log.v(TAG, "GPS ENABLED"); 
     manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
       1000L, 50.0f, mGPSLocationListener); 
    } 

    myLocTimer = new Timer("LocationRunner", true); 
    myLocTimer.schedule(mTimerTask, 0, 500); 
} 

GPSStatusListener

private GpsStatus.Listener mGPSStatusListener = new GpsStatus.Listener() { 

    @Override 
    public synchronized void onGpsStatusChanged(int event) { 
     switch (event) { 
     case GpsStatus.GPS_EVENT_SATELLITE_STATUS: 
      Log.v(TAG, "GPS SAtellitestatus"); 
      GpsStatus status = manager.getGpsStatus(null); 
      mSattelites = 0; 
      Iterable<GpsSatellite> list = status.getSatellites(); 
      for (GpsSatellite satellite : list) { 
       if (satellite.usedInFix()) { 
        mSattelites++; 
       } 
      } 
      break; 

     case GpsStatus.GPS_EVENT_FIRST_FIX: 
      /* 
      * Toast.makeText(getApplicationContext(), "Got First Fix", 
      * Toast.LENGTH_LONG).show(); 
      */ 
      break; 

     case GpsStatus.GPS_EVENT_STARTED: 
      /* 
      * Toast.makeText(getApplicationContext(), "GPS Event Started", 
      * Toast.LENGTH_LONG).show(); 
      */ 
      break; 

     case GpsStatus.GPS_EVENT_STOPPED: 
      /* 
      * Toast.makeText(getApplicationContext(), "GPS Event Stopped", 
      * Toast.LENGTH_LONG).show(); 
      */ 
      break; 
     default: 
      break; 
     } 
    } 
}; 

LocationListener的

public class GPSLocationListener implements LocationListener { 

    @Override 
    public void onLocationChanged(Location argLocation) { 
     location = argLocation; 
    } 

    public void onProviderDisabled(String provider) { 

    } 

    public void onProviderEnabled(String provider) { 

    } 

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

    } 
} 

的TimerTask類

class LocTimerTask extends TimerTask { 
    String provider; 

    public LocTimerTask(String provider) { 
     this.provider = provider; 
    } 

    final Handler mHandler = new Handler(Looper.getMainLooper()); 

    Runnable r = new Runnable() { 
     @Override 
     public void run() { 
      i++; 
      Log.v(TAG, "Timer Task run" + i); 
      location = manager.getLastKnownLocation(provider); 

      if (location != null) { 
       Log.v(TAG, "in timer task run in if location not null"); 
       isGPS = true; 
       onLocationReceived(location); 
       myLocTimer.cancel(); 
       myLocTimer.purge(); 
       mTimerTask.cancel(); 
       return; 
      } else { 
       Log.v(TAG, "in timer task run in else location null"); 
       isGPS = false; 
       if (location == null && i == MAX_ATTEMPTS) { 
        Log.v(TAG, "if 1 max attempts done"); 
        turnGPSOff(); 
        location = manager 
          .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
        if (location != null) { 
         Log.v(TAG, 
           "if 1 max attempts done Location from network not null"); 
         Log.v(TAG, 
           "if 1 max attempts done Location from network not null coordinates not null"); 
         onLocationReceived(location); 
         myLocTimer.cancel(); 
         myLocTimer.purge(); 
         mTimerTask.cancel(); 
         return; 
        } 
       } else { 
        return; 
       } 
      } 
      i = 0; 
     } 
    }; 

    public void run() { 
     mHandler.post(r); 
    } 
} 

這裏計時器已經計劃在每500毫秒運行一次。意思是,每500毫秒計時器任務的run method將被執行。在運行方法中,嘗試從GPS提供商處獲取具體編號的位置。的嘗試(這裏MAX_ATTEMPTS)說5或10.如果它獲得指定的編號。的嘗試然後使用該位置,否則如果計數器(這裏我)的值已超過MAX_ATTEMPTS,則從網絡提供商獲取位置。在獲取位置信息時,我已將該位置傳遞給回撥方法onLocationReceived(Location mLoc),您可以在該位置進一步處理位置數據。這裏是你將如何使用回調方法:

監聽

public interface OnLocationReceivedListener { 
public void onLocationReceived(Location mLoc); //callback method which will be defined in your class. 

}

你的類應該實現上述定義的監聽器。在你的班級:

@Override 
public void onLocationReceived(Location mLoc) { 
    //Do your stuff 
} 

希望它有幫助。如果有人有更好的方法,請讓我知道。