反向地理编码不起作用

时间:2012-02-19 09:47:20

标签: android gps

我写下以下代码来获取当前位置。虽然我在不同纬度和经度的模拟器中测试它。但它无法在真实位置转换纬度和经度。

          public class LocationFindingActivity extends Activity {
/** Called when the activity is first created. */

EditText et;
LocationManager  locationManager;
String provider;
LocationListener locationListener;
Location currentLocation;
String addressString="";
String longlattString="";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et=(EditText)findViewById(R.id.locationTXT);

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

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


    et.setText(addressString);
    locationListener =new LocationListener() {

        @Override
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            // TODO Auto-generated method stub

        }

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

        }

        @Override
        public void onProviderDisabled(String arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLocationChanged(Location arg0) {
            // TODO Auto-generated method stub
            updateWithNewLocation(currentLocation);

        }
    };
    updateWithNewLocation(currentLocation);
     locationManager.requestLocationUpdates(provider, 2000, 10,locationListener)

}



private void updateWithNewLocation(Location location) {




    if (location != null) 
    {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      longlattstring="Lattitude :"+lat+"Longitude :"+lng;


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

     try 
     {
            List<Address> addresses = gc.getFromLocation(lat, lng, 1);
            StringBuilder sb = new StringBuilder();
            //Toast.makeText(this, "Problem1", 2000).show();
            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");
                    Toast.makeText(this, "Problem2", 2000).show();
                    sb.append(address.getPostalCode()).append("\n");
                    sb.append(address.getCountryName());
            }
            addressString = sb.toString();
     } 
     catch (IOException e) 
     {
         Toast.makeText(this, "Problem Catch", 2000).show();
     }
}


    else 
    {
        addressString = "No location found";    
    }

    et.setText(addressString);


}

}

我在这一行遇到问题

列表地址= gc.getFromLocation(lat,lng,1);

声明不会返回任何内容。

1 个答案:

答案 0 :(得分:0)

这是一个他们从未修复的已知错误,请参阅service not avavilable。我认为您会发现它在API级别7模拟器中有效。