我从当前位置获取纬度和经度,并使用地理编码器从纬度经度显示城市国家和州名
日志错误
05-15 12:13:40.584: E/Running(309): Mehod getBestProvider
05-15 12:13:40.674: E/Running(309): Mehod getBestProvider
05-15 12:13:57.083: E/Running(309): C Latitude27.422006
05-15 12:13:57.083: E/Running(309): C Longitude22.084095
05-15 12:13:57.093: E/Running(309): try called
05-15 12:13:57.093: E/Running(309): Exception called
05-15 12:13:57.093: W/System.err(309): java.io.IOException: Service not Available
05-15 12:13:57.113: W/System.err(309):at
ndroid.location.Geocoder.getFromLocation(Geocoder.java:117)
05-15 12:13:57.113: W/System.err(309): at
活动类
public class CitycountrymapActivity extends Activity implements LocationListener {
private LocationManager locationManager;
//private GeoPoint currentPoint;
private Location currentLocation;
double currentlatitude;
double currentlongitude;
String provider;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getLocation();
}
public void getLocation()
{
provider=getBestProvider();
currentLocation=locationManager.getLastKnownLocation(provider);
if(currentLocation!=null)
{
setCurrentLocation(currentLocation);
}
else
{
Toast.makeText(this, "Location not yet acquired", Toast.LENGTH_LONG).show();
}
}
public String getBestProvider()
{
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
String bestprovider=locationManager.getBestProvider(criteria, true);
Log.e("Running","Mehod getBestProvider");
return bestprovider;
}
public void setCurrentLocation(Location location)
{
//currentlatitude=(double)(location.getLatitude());
//currentlongitude=(double)(location.getLongitude());
currentlatitude =27.422006;
currentlongitude=22.084095;
currentLocation = new Location("");
//currentPoint = new GeoPoint((int)(72.454565* 1E6),(int)(23.4572448 * 1E6));
//currentPoint = new GeoPoint((int)(72.454565),(int)(23.4572448));
Global.clat=currentlatitude;
Global.clon=currentlongitude;
Log.e("Running","C Latitude"+Global.clat);
Log.e("Running","C Longitude"+Global.clon);
try
{
Log.e("Running","try called");
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses=geocoder.getFromLocation(currentlatitude, currentlongitude, 1);
Log.e("Running","geocoder set");
Log.e("Running","addresses");
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);
Log.e("Running","Latitude"+currentlatitude);
Log.e("Running","Longitude"+currentlongitude);
Log.e("Running","Address"+address);
Log.e("Running","City-->"+city);
Log.e("Running","Country->"+country);
}
catch(Exception e)
{
Log.e("Running","Exception called");
e.printStackTrace();
}
}
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Toast.makeText(this, "Loction Changed", Toast.LENGTH_SHORT).show();
setCurrentLocation(location);
}
protected void onResume()
{
super.onResume();
locationManager.requestLocationUpdates(getBestProvider(), 1000, 1, this);
}
protected void onPause()
{
super.onPause();
locationManager.removeUpdates(this);
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Provider Disabled", Toast.LENGTH_SHORT).show();
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Provider Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Toast.makeText(this, "Staus Changed", Toast.LENGTH_SHORT).show();
}
}