2015-06-18 54 views
-1

我正在嘗試創建一個計算銷售稅的Android應用程序(這取決於您所處的州或省),但我無法弄清楚如何獲取什麼州/省Android用戶目前在如何獲取用戶當前狀態(位置)

+0

可能重複[如何從經度和緯度獲取完整的地址?](http://stackoverflow.com/questions/9409195/how-to-get-complete-address-from-latitude-and-longitude) –

+0

你應該看看地址類http://developer.android.com/reference/android/location/Address.html –

回答

-1

這可能是最好的辦法。

首先我檢查供應商啓用的。有些可能在設備上被禁用,有些可能在應用程序清單中被禁用。 如果有任何提供者可用,我啓動位置偵聽器和超時定時器。在我的例子中是20秒,可能不夠GPS,所以你可以放大它。 如果我從位置偵聽器獲取更新,則使用提供的值。我停止了聽衆和計時器。 如果我沒有得到任何更新和計時器,我必須使用上次已知的值。 我從可用提供程序中獲取最新的已知值並選擇最近的值。 以下是我用我的類:

LocationResult locationResult = new LocationResult(){ 
    @Override 
    public void gotLocation(Location location){ 
     //Got the location! 
    } 
}; 
MyLocation myLocation = new MyLocation(); 
myLocation.getLocation(this, locationResult); 


import java.util.Timer; 
import java.util.TimerTask; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 

public class MyLocation { 
    Timer timer1; 
    LocationManager lm; 
    LocationResult locationResult; 
    boolean gps_enabled=false; 
    boolean network_enabled=false; 

    public boolean getLocation(Context context, LocationResult result) 
    { 
     //I use LocationResult callback class to pass location value from MyLocation to user code. 
     locationResult=result; 
     if(lm==null) 
      lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

     //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) 
      return false; 

     if(gps_enabled) 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps); 
     if(network_enabled) 
      lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork); 
     timer1=new Timer(); 
     timer1.schedule(new GetLastLocation(), 20000); 
     return true; 
    } 

    LocationListener locationListenerGps = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      timer1.cancel(); 
      locationResult.gotLocation(location); 
      lm.removeUpdates(this); 
      lm.removeUpdates(locationListenerNetwork); 
     } 
     public void onProviderDisabled(String provider) {} 
     public void onProviderEnabled(String provider) {} 
     public void onStatusChanged(String provider, int status, Bundle extras) {} 
    }; 

    LocationListener locationListenerNetwork = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      timer1.cancel(); 
      locationResult.gotLocation(location); 
      lm.removeUpdates(this); 
      lm.removeUpdates(locationListenerGps); 
     } 
     public void onProviderDisabled(String provider) {} 
     public void onProviderEnabled(String provider) {} 
     public void onStatusChanged(String provider, int status, Bundle extras) {} 
    }; 

    class GetLastLocation extends TimerTask { 
     @Override 
     public void run() { 
      lm.removeUpdates(locationListenerGps); 
      lm.removeUpdates(locationListenerNetwork); 

      Location net_loc=null, gps_loc=null; 
      if(gps_enabled) 
       gps_loc=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      if(network_enabled) 
       net_loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

      //if there are both values use the latest one 
      if(gps_loc!=null && net_loc!=null){ 
       if(gps_loc.getTime()>net_loc.getTime()) 
        locationResult.gotLocation(gps_loc); 
       else 
        locationResult.gotLocation(net_loc); 
       return; 
      } 

      if(gps_loc!=null){ 
       locationResult.gotLocation(gps_loc); 
       return; 
      } 
      if(net_loc!=null){ 
       locationResult.gotLocation(net_loc); 
       return; 
      } 
      locationResult.gotLocation(null); 
     } 
    } 

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

例如,如果你從網絡提供商處獲取更新不停止聽衆而是繼續等待。 GPS提供更準確的數據,因此值得等待。如果計時器過去了,並且您已從網絡獲取更新,但無法從GPS獲得更新,則可以使用網絡提供的值。

+1

我沒有讀得超級密切,但我認爲這讓你在那裏。然後,您需要反轉地理座標以獲取狀態。 – nasch

+0

謝謝一堆!!!! –

+0

如果它的工作標記已更正,以便將來可能有用,我很高興它有幫助 –