2011-09-03 104 views
1

有沒有一種可能的方式來使用GPS提供商快速檢測新的位置?GPS提供商及其性能

+0

使用網絡提供商...... – st0le

+0

謝謝,但爲什麼GPS提供商很慢,檢測的位置? – Dev

+0

謝謝stl0e&Jonas ... – Dev

回答

6

GPS通常很慢 - 就是這樣。但也有幾件事情可以爲了加快用戶的體驗做到:

  • 立即開始尋找一個位置的應用程序啓動 - 不要等到他們到了那需要一個活動位置

  • 使用網絡供應商,這往往要快得多

  • 使用被動提供商。即接收已被設備上的其他應用程序獲取的位置更新

  • 使用最後已知位置 - 將其存儲一段時間,如果存在,則立即爲您提供位置(雖然它可能有點舊)

  • 如果適用於您的應用程序的情況下,保存在首選項

本文章由谷歌的雷託邁耶是一個偉大的地方開始自己的位置 - 看看在示例代碼和詳細的相關鏈接 http://blog.radioactiveyak.com/2011/06/how-to-build-location-based-apps-that.html

+0

thinks olie c,但我認爲網絡提供商不比gps提供商更準確。我如何使用lastknown位置來檢測新位置? – Dev

+2

查看本頁面的「快速修復最後一次已知位置」。這很容易http://developer.android.com/guide/topics/location/obtaining-user-location.html –

0

U可以通過網絡提供商和GPS提供商檢索位置。

如果你想快速檢索位置,你可以同時檢查兩個提供者。 這裏是代碼..

創建一類像這首:

import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.Handler; 
import android.provider.Settings; 
import android.util.Log; 
import android.widget.Toast; 

import com.tcs.android.eventinfo.data.DataStore; 



public class GeoFence { 
// public Timer timer1; 
LocationManager lm; 
LocationResult locationResult; 
boolean gps_enabled=false; 
boolean network_enabled=false; 
Context mContext; 

public Handler handler; 
public GetLastLocation r; 
public boolean isLocationNullSecondTime=false; 

public GeoFence(Context context) { 
    mContext=context; 
    handler = new Handler(); 
    chekStatusOfLocationProvider(isLocationNullSecondTime); 
} 
public void chekStatusOfLocationProvider(boolean isLocationNullSecondTime){ 
    if(lm==null) 
     lm = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); 

    Log.i("GPS"," GPS"+lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)); 
    Log.i("GPS"," Network"+lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)); 
    Log.i("GPS"," Passive"+lm.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)); 

    if ((!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) 
      && !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
      || isLocationNullSecondTime) { 
     Log.i("GPS", "Inside Constructor"); 

     Toast.makeText(mContext, "Turn on your Location provider", 
       Toast.LENGTH_LONG).show(); 
     Intent myIntent = new Intent(
       Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
     mContext.startActivity(myIntent); 
    } 


} 

public boolean getLocation(LocationResult result) 
{ 
    Log.i("GEo fence","DataStore.IS_THREAD_RUNNING : "+DataStore.IS_THREAD_RUNNING); 
    if(!DataStore.IS_THREAD_RUNNING) { 


/*  if ((!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) 
       && !lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) 
       || isLocationNullSecondTime) { 
      Log.i("GPS", "Inside Constructor"); 

      Toast.makeText(mContext, "Turn on your Location provider", 
        Toast.LENGTH_LONG).show(); 
      Intent myIntent = new Intent(
        Settings.ACTION_LOCATION_SOURCE_SETTINGS); 
      mContext.startActivity(myIntent); 
     }*/ 

    Log.i("GPS","inside getlocation"); 
    //I use LocationResult callback class to pass location value from MyLocation to user code. 
    locationResult=result; 
    //exceptions will be thrown if provider is not permitted. 
    try{gps_enabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);}catch(Exception ex){} 
    try{network_enabled=lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);}catch(Exception ex){} 

    //don't start listeners if no provider is enabled 
    if(!gps_enabled && !network_enabled){ 
     Log.i("GPS","nothing is enable"); 
     return false; 
    } 
    if(gps_enabled){ 
     Log.i("GPS","inside getlocation Gps is enabled"); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 5000, locationListenerGps); 
    } 
    if(network_enabled){ 
     Log.i("GPS","inside getlocation netwok is enable"); 
     lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0, 5000, locationListenerNetwork); 
    }  
    r=new GetLastLocation(); 
    handler.postDelayed(r,20000); 
    return true; 
} 
else { 
    return false; 
}} 


LocationListener locationListenerGps = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     DataStore.IS_THREAD_RUNNING=true; 
     Log.i("GPS","inside onLocationcahnged of gps"); 
     isLocationNullSecondTime=false; 
     lm.removeUpdates(this); 
     lm.removeUpdates(locationListenerNetwork); 
     handler.removeCallbacks(r); 
     locationResult.gotLocation(location); 

    } 
    public void onProviderDisabled(String provider) { Log.i("GPS","inside onProviderDisabled of gps");} 
    public void onProviderEnabled(String provider) { Log.i("GPS","inside onProviderEnabled of gps");} 
    public void onStatusChanged(String provider, int status, Bundle extras) {} 
}; 

LocationListener locationListenerNetwork = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     DataStore.IS_THREAD_RUNNING=true; 
     Log.i("GPS","inside onLocationcahnged of Network"); 
     isLocationNullSecondTime=false; 
     lm.removeUpdates(this); 
     lm.removeUpdates(locationListenerGps); 
     handler.removeCallbacks(r); 
     locationResult.gotLocation(location); 

    } 
    public void onProviderDisabled(String provider) {Log.i("GPS","inside onProviderDisabled of Network");} 
    public void onProviderEnabled(String provider) {Log.i("GPS","inside onProviderEnabled of Network");} 
    public void onStatusChanged(String provider, int status, Bundle extras) {} 
}; 

class GetLastLocation implements Runnable { 
    @Override 
    public void run() { 
     DataStore.IS_THREAD_RUNNING=true; 
     Log.i("GPS","inside the thread run"); 
     lm.removeUpdates(locationListenerGps); 
     lm.removeUpdates(locationListenerNetwork); 

     Location net_loc=null, gps_loc=null; 
     if(gps_enabled){ 
      gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      Log.i("GPS","gps location object :"+gps_loc); 
     } 
     if(network_enabled){ 
      net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      Log.i("GPS","neteork object :"+ net_loc); 
     } 
     //if there are both values use the latest one 
     if(gps_loc!=null && net_loc!=null){ 
      handler.removeCallbacks(r); 
      isLocationNullSecondTime=false; 
      Log.i("GPS","neteork object not null :"+ net_loc); 
      Log.i("GPS","GPS object not null :"+ gps_loc); 
      if(gps_loc.getTime()>net_loc.getTime()) 
       locationResult.gotLocation(gps_loc); 
      else 
       locationResult.gotLocation(net_loc); 
      return; 
     }else{ 
      handler.removeCallbacks(r); 
      if(gps_loc!=null){ 
       isLocationNullSecondTime=false; 
       locationResult.gotLocation(gps_loc); 
       return; 
      } 
      else if(net_loc!=null){ 
       isLocationNullSecondTime=false; 
       locationResult.gotLocation(net_loc); 
       return; 
      } 
      else{ 
       isLocationNullSecondTime=true; 
       Log.i("GPS","Both the object is null and asking for turn on the Location provider :"); 
       locationResult.gotLocation(null); 
       return; 
      } 
     } 



     //locationResult.gotLocation(null); 
    } 
} 

public static abstract class LocationResult{ 
    public abstract void gotLocation(Location location); 
} 

然後在活動編寫代碼或實現這樣的gotLocation(位置定位)方法:

public LocationResult locationResult = new LocationResult() { 

    @Override 
    public void gotLocation(final Location location) { 
     // do something 
     mLocation = location; 
     if (geoFence.handler != null) { 
      geoFence.handler.removeCallbacks(geoFence.r); 
     } 
     if (location == null) { 
      if (DataStore.IS_THREAD_RUNNING) { 
       DataStore.IS_THREAD_RUNNING=false; 
      } 
      if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
       createAlert("Turn on ur GPS provider", new int[] { 
         R.string.yes, R.string.no },2); 
       return; 
      } 
      if (!lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
       createAlert("Turn on ur Network provider", new int[] { 
         R.string.yes, R.string.no },2); 
       return; 
      } 
      Toast.makeText(PreHomeScreen.this, 
        "Turn on your both Location provider and try again ", 
        Toast.LENGTH_LONG).show(); 

     } else { 
//////////////////////////////////Do ur work///////////////////////////////////// 

}

} 
}; 
0

我的博客包含如何明星的示例代碼t位置服務並等待工作線程中的位置。您可以在應用程序啓動時開始搜索位置,但在後臺等待時繼續運行應用程序。在需要位置之前,用戶可能需要做一些事情,並且此時位置服務可以返回可用位置。

http://www.scotthelme.co.uk/blog/android-location-services/