2013-07-10 72 views
3

我有一個地理編碼器,GCD,這行代碼是反向地理編碼縮小地理編碼地址城市僅

List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1); 
        if(addresses != null) { 
         Address returnedAddress = addresses.get(0); 

         for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { 
         strReturnedAddress = (returnedAddress.getAddressLine(i)).toString(); 

strReturnedAddress返回類似

10453中央布朗克斯,紐約市,紐約州

我只需要城市名稱,這是紐約市。

刪除字符串的部分將非常困難,因爲地理編碼的輸出可能會更改。我只需要地理編碼就可以給我這座城市。

我檢查了http://developer.android.com/reference/android/location/Geocoder.html但找不到答案。

回答

5

真棒,你發現自己的解決方案。

您可能雖然想使用下面的方法,這是比較簡單的,因爲它利用了,而不必通過地址列表來遍歷位置API:

Geocoder geocoder; 
List<Address> addresses; 
geocoder = new Geocoder(this, Locale.getDefault()); 
addresses = geocoder.getFromLocation(latitude, longitude, 1); 

String address = addresses.get(0).getAddressLine(0); 
String city = addresses.get(0).getAddressLine(1); 
String country = addresses.get(0).getAddressLine(2); 

即使代碼is supplied by another user,我有我自己實施了這個確切的方法,並發現它很有用。

希望這會有所幫助。

+0

謝謝。甚至在我找到解決方案後回答道具。 –

+0

不用擔心男人,認爲它會幫助你出於簡單起見 – Demitrian

1

我自己找到了解決方案,我將其分享給有需要的人以供將來參考。

當地址被定義時,我需要做的就是address.getLocality()作爲城市名稱。

對於這種情況下它會

Address returnedAddress = addresses.get(0); 
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { 
         strReturnedAddress = (returnedAddress.getLocality().toString(); 
0
public class Locationfinder extends Activity implements LocationListener{ 
    private TextView latituteField,longitudeField, Address; 
     private LocationManager locationManager; 
     private String provider; 
    List<Address> mAddresses; 
     double lat,lng; 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      latituteField = (TextView) findViewById(R.id.TextView02); 
      longitudeField = (TextView) findViewById(R.id.TextView04); 
      Address=(TextView)findViewById(R.id.TextView03); 

      locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

      Criteria criteria = new Criteria(); 
      provider = locationManager.getBestProvider(criteria, false); 
      Location location = locationManager.getLastKnownLocation(provider); 

      // Initialize the location fields 
      if (location != null) { 

       onLocationChanged(location); 
      } else { 
       latituteField.setText("Location not available"); 
       longitudeField.setText("Location not available"); 
      } 
     } 

     protected void onResume() { 
      super.onResume(); 
      locationManager.requestLocationUpdates(provider, 400, 1, this); 
      } 

//   Remove the locationlistener updates when Activity is paused 
      @Override 
      protected void onPause() { 
      super.onPause(); 
      locationManager.removeUpdates(this); 
      } 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 

     lat = (double) (location.getLatitude()); 
     lng = (double) (location.getLongitude()); 
     System.out.println("lat1: " + lat +" " +"lng1" +lng); 
     latituteField.setText(String.valueOf(lat)); 
     longitudeField.setText(String.valueOf(lng)); 

     Geocoder gcd = new Geocoder(getApplicationContext(), 
       Locale.getDefault()); 
     try { 


       mAddresses = gcd.getFromLocation(lat,lng, 1); 

      String address = mAddresses.get(0).getAddressLine(0); 
       String city = mAddresses.get(0).getAddressLine(1); 
       String country = mAddresses.get(0).getAddressLine(2); 

       Address.setText("Address:- " + address + "city :" +city + "country : "+ country); 


     } catch (IOException e) { 
     e.printStackTrace(); 
     latituteField.setText("Location not available"); 
      longitudeField.setText("Location not available"); 
     } 
    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "Disable GPS " + provider, 
       Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 
     Toast.makeText(this, "Enable GPS " + provider, 
       Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 

} 
+0

GPS定位器的當前地址..甜蜜的編碼:) – elamathy

相關問題