2014-03-02 138 views
0

我已經實現了這個代碼來顯示基於GPS的地址。經度和緯度工作正常(在屏幕上出現),但是,它的立場「沒有地址找到」的地址。請看這個代碼並指出任何錯誤。由於如何從經度和緯度獲取地址?

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_find__location); 

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

    Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    criteria.setAltitudeRequired(false); 
    criteria.setBearingRequired(false); 
    criteria.setCostAllowed(true); 
    criteria.setPowerRequirement(Criteria.POWER_LOW); 
    String provider = locationManager.getBestProvider(criteria, true); 

    //String provider = LocationManager.GPS_PROVIDER; 
    Location location = locationManager.getLastKnownLocation(provider); 
    updateWithNewLocation(location); 

    locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); 

    //buttons assigned 
    Button mainMenuBtn = (Button) findViewById(id.mainMenuBtn); 

    mainMenuBtn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      finish(); 


     } 
    }); 
} 

    private final LocationListener locationListener = new LocationListener() { 

      @Override 
    public void onLocationChanged(Location location) { 
     updateWithNewLocation(location); 

    } 

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

    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onProviderDisabled(String provider) { 
     updateWithNewLocation(null); 

    } 


}; 

private void updateWithNewLocation(Location location) { 
    String latLong; 
    TextView myLocationText; 
    myLocationText = (TextView)findViewById(R.id.myLocationText); 

    String addressString = "no address found"; 

    if(location != null){ 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
     latLong = "Lat: "+lat+"\nLong: "+lng; 

     //double latitude = location.getLatitude(); 
     //double longitude = location.getLongitude(); 


     Geocoder gc = new Geocoder(this,Locale.getDefault()); 
     try{ 

      List<Address> addresses = gc.getFromLocation(lat, lng, 1); 
      StringBuilder sb = new StringBuilder(); 
      if (addresses.size() > 0){ 
       Address address = addresses.get(0); 

       for(int i=0; i < address.getMaxAddressLineIndex(); i++) 

        sb.append(address.getAddressLine(i)).append("\n"); 
        sb.append(address.getLocality()).append("\n"); 
        sb.append(address.getPostalCode()).append("\n"); 
        sb.append(address.getCountryName()); 
      } 

       addressString = sb.toString(); 

     }catch (IOException e){} 
    }else{ 
     latLong = "No location found"; 
    } 

    myLocationText.setText("Your coordinates are:\n"+latLong + "\n"+addressString); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.find__location, menu); 
    return true; 
} 

}

回答

0

我有這是很煩人的同樣的問題。一些答案表明geocoder並不總是返回一個值,所以你必須循環一些。然而,這不是一個好的方法,因爲你可能永遠不會得到一個價值,有時是這樣的。因此,我在我的項目中所做的就是使用谷歌的反向地理編碼api。想法是一樣的,但方法是不同的。

這是API頁面:https://developers.google.com/maps/documentation/geocoding/

檢查第一個答案在這裏:Android Geocoder getFromLocationName always returns null 希望這有助於