Android 在服务中使用地理编码器获取位置地址
Android Get Location Address with Geocoder in a Service
我跟随this获取了位置坐标和地址
所以这里它每 10 秒更新一次位置坐标纬度和经度
我正在尝试与他们一起获取位置地址
在这里获取我正在使用的地址 Const class
public class Const {
public static String getCompleteAddressString(Context m_context, double LATITUDE, double LONGITUDE) {
String strAdd = "";
Geocoder geocoder = new Geocoder(m_context, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
strAdd = strReturnedAddress.toString();
Log.v("My Current location add", "" + strAdd.toString());
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(m_context, "Sorry, Your location cannot be retrieved !" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
return strAdd;
}
}
现在 Main Activity 要获取我正在使用的地址
private void updateUI() {
mLatitudeTextView.setText(String.format("%s: %f", mLatitudeLabel,
mCurrentLocation.getLatitude()));
mLongitudeTextView.setText(String.format("%s: %f", mLongitudeLabel,
mCurrentLocation.getLongitude()));
mLastUpdateTimeTextView.setText(String.format("%s: %s", mLastUpdateTimeLabel,
mLastUpdateTime));
//For Address
mLocAddTextView.setText(String.format("%s: %s", mLocAddressLabel,
Const.getCompleteAddressString(this, mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude())));
}
但是这里没有任何显示,任何人都可以告诉我这有什么问题
你的代码很完美
在您的 Const class
中试试这个
strReturnedAddress .append(returnedAddress.getAddressLine(0)).append("\n");
如果您想使用邮政编码、地区和国家...等等...使用
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
strReturnedAddress .append(returnedAddress.getAddressLine(0)).append("\n");
strReturnedAddress .append(returnedAddress.getLocality()).append("\n");
strReturnedAddress .append(returnedAddress.getPostalCode()).append("\n");
strReturnedAddress .append(returnedAddress.getCountryName());
strAdd = strReturnedAddress.toString();
Log.v("My Current location add", "" + streturnedAddressd.toString());
}
要获取 Geocoder 地址,请遵循 Android GeoCode Location
我跟随this获取了位置坐标和地址
所以这里它每 10 秒更新一次位置坐标纬度和经度
我正在尝试与他们一起获取位置地址
在这里获取我正在使用的地址 Const class
public class Const {
public static String getCompleteAddressString(Context m_context, double LATITUDE, double LONGITUDE) {
String strAdd = "";
Geocoder geocoder = new Geocoder(m_context, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
strAdd = strReturnedAddress.toString();
Log.v("My Current location add", "" + strAdd.toString());
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(m_context, "Sorry, Your location cannot be retrieved !" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
return strAdd;
}
}
现在 Main Activity 要获取我正在使用的地址
private void updateUI() {
mLatitudeTextView.setText(String.format("%s: %f", mLatitudeLabel,
mCurrentLocation.getLatitude()));
mLongitudeTextView.setText(String.format("%s: %f", mLongitudeLabel,
mCurrentLocation.getLongitude()));
mLastUpdateTimeTextView.setText(String.format("%s: %s", mLastUpdateTimeLabel,
mLastUpdateTime));
//For Address
mLocAddTextView.setText(String.format("%s: %s", mLocAddressLabel,
Const.getCompleteAddressString(this, mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude())));
}
但是这里没有任何显示,任何人都可以告诉我这有什么问题
你的代码很完美
在您的 Const class
strReturnedAddress .append(returnedAddress.getAddressLine(0)).append("\n");
如果您想使用邮政编码、地区和国家...等等...使用
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
strReturnedAddress .append(returnedAddress.getAddressLine(0)).append("\n");
strReturnedAddress .append(returnedAddress.getLocality()).append("\n");
strReturnedAddress .append(returnedAddress.getPostalCode()).append("\n");
strReturnedAddress .append(returnedAddress.getCountryName());
strAdd = strReturnedAddress.toString();
Log.v("My Current location add", "" + streturnedAddressd.toString());
}
要获取 Geocoder 地址,请遵循 Android GeoCode Location