【发布时间】:2011-05-13 07:30:57
【问题描述】:
我编写了以下代码来返回我当前的位置地址。但只有手动打开我的 Wifi 时,我才能获得这些值。但我想打开 Gps 并从 GPS 而不是 Wifi 获取我的地址。有人请告诉我要在此代码中修改什么。以下是用于定位我当前地址的代码。提前致谢。
public String myloc()
{
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(criteria, true);
// Update the GUI with the last known
Location location = locationManager.getLastKnownLocation(provider);
if(location!=null)
{
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
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);
sb.append(address.getAddressLine(0)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getAdminArea()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
}
catch (IOException e)
{
}
}
else
{
latLongString = "No location found";
}
return addressString;
}
【问题讨论】: