2014-02-24 113 views
2

問題的標題幾乎總結了我想問的問題。所以我有了使用LocationManager.NETWORK_PROVIDER獲得當前城市名稱的代碼,它的作用就像一個魅力。然而,一個問題突然出現:「如果電話無法使用LocationManager.NETWORK_PROVIDER獲得城市名稱,會發生什麼?」。當然,我發現該電話:由於某些原因,聯想手機不能使用LocationManager.NETWORK_PROVIDER獲得座標。所以我的問題我怎樣才能讓我的應用程序第一次使用LocationManager.NETWORK_PROVIDER查找座標以獲取城市的名稱,如果結果爲空以獲取使用LocationManager.GPS_PROVIDER的座標?Andorid如何使用座標獲取當前城市的名稱

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_where_am_i); 

    if (android.os.Build.VERSION.SDK_INT > 9) 
    { 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
    } 


    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);  

} 

,我得到這個城市的名稱爲:

public void onLocationChanged(Location location) 
{  
    curLatitude = location.getLatitude(); 
    curLongitude = location.getLongitude();  

    try{ 
     //Get the name of the city the user is currently in base on the current latitude and Longitude 
      Geocoder gcd = new Geocoder(this, Locale.UK); 
      List<Address> addresses = gcd.getFromLocation(curLatitude, curLongitude,1); 

      if (addresses.size() > 0) 
      { 
       StringBuilder cityName = new StringBuilder();     
       cityName.append(addresses.get(0).getLocality()); 
       CityName = cityName.toString();     
       //Check if the user is in allowed cities     
      }    

     } 
    catch(IOException ex) 
     { 
      ex.printStackTrace(); 
     } 

} 
+0

訪問http://upadhyayjiteshandroid.blogspot.in/2013/02/convert-celllocation-to-real-location.html和HTTP ://upadhyayjiteshandroid.blogspot.in/2013/02/android-get-location-without-gps-using.html可能會幫助你 –

+0

[此鏈接](http://hejp.co.uk/android/android-gps例如)可能對你有幫助。嘗試'GPSTracker'獲取'lat'和'lng'而不是地理位置管理器 –

+0

,但我想要的是在GPS和網絡提供商 – user3182266

回答

0

使用此參數:

  1. getPremises();
  2. getSubAdminArea();
    3.getSubLocality();

If you want in detail you should prefer

+0

之間切換,這很棒,但這將如何幫助我在提供商之間切換? – user3182266

+0

這是您自己的編程方式,我認爲您應該使用方法併爲兩個提供者調用onLocationChanged()。 – Yogendra

0

當位置更新是它不保證返回位置更新供應商提出要求。 onLocationChanged()方法僅在位置更改時調用。如果您將使用Network_provider獲取位置更新,則返回位置更新將需要很長時間。在這種情況下,你可能會啓動一個計時器,當它到期時,你可能會從GPS_provider獲得位置更新。否則,只是簡單的做如下

myLocObj = new MyLocationListener(); 
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

nw = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
if(nw){ 
    Toast.makeText(BasicArchitectActivity.this, "fetching from NW", Toast.LENGTH_LONG).show(); 
    manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, myLocObj); 
} 

gps = manager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
if(gps){ 
    Toast.makeText(BasicArchitectActivity.this, "fetching from GPS", Toast.LENGTH_LONG).show(); 
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocObj); 
} 


if(!gps && !nw){ 
    Toast.makeText(MainActivity.this, "Please enable GPS and Network positioning in your Settings ", Toast.LENGTH_LONG).show(); 
} 

這裏的位置監聽器

class MyLocationListener implements LocationListener{ 

    double currentlat, currentlng; 

    @Override 
    public void onLocationChanged(Location location) { 

     if(location != null){ 

      currentlat = location.getLatitude(); 
      currentlng = location.getLongitude(); 

      Toast.makeText(BasicArchitectActivity.this, "Current location \nlat= " +currentlat+", lng= " + currentlng , Toast.LENGTH_SHORT).show(); 

    } 

} 
相關問題