我是Android新手。我开发了一个必须显示当前位置名称的应用程序。我试着使用Geocoder类。但似乎在一些手机中显示正确的位置,但有些显示任何位置并且变为空。
我的代码在这里,
Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
List<Address> listAddresses = null;
try {
listAddresses = geocoder.getFromLocation(gps.getLatitude(), gps.getLongitude(), 1);
} catch (IOException e) {
e.printStackTrace();
}
if (null != listAddresses && listAddresses.size() > 0) {
Location = "";
for (int i = 0; i < 4; i++) {
if (listAddresses.get(0).getAddressLine(i) != null) {
Location += listAddresses.get(0).getAddressLine(i) + ",";
}
}
Log.e("Location", Location);
}
我的舱单课程,
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
我无法理解这是什么问题?它取决于不同的设备吗?还是其他任何原因?
答案 0 :(得分:0)
**Just pass your current lat-long in this method and you will get address:**
private void getAddress(double locationLatitude, double locationLongitude) {
Geocoder gCoder = new Geocoder(getActivity());
try {
final List<Address> list = gCoder.getFromLocation(locationLatitude, locationLongitude, 1);
if (list != null && list.size() > 0) {
Address address = list.get(0);
StringBuilder sb = new StringBuilder();
if (address.getAddressLine(0) != null) {
sb.append(address.getAddressLine(0)).append("\n");
}
sb.append(address.getLocality()).append(",");
// sb.append(address.getPostalCode()).append(",");
sb.append(address.getCountryName());
strAddress = sb.toString();
strAddress = strAddress.replace(",null", "");
strAddress = strAddress.replace("null", "");
strAddress = strAddress.replace("Unnamed", "");
}
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
if (!TextUtils.isEmpty(strAddress)) {
Logg.e("address++++", "" + strAddress);
edtZipcode.setFocusable(false);
edtZipcode.setFocusableInTouchMode(false);
edtZipcode.setText(strAddress); // set your current address in this edittext using your current lat-long
edtZipcode.setFocusable(true);
edtZipcode.setFocusableInTouchMode(true);
} else {
Logg.e("address", "");
edtZipcode.setText("");
Toast.show(getActivity(), getResources().getString(R.string.toast_enter_zipcode));
new Animation(Techniques.Shake, 1000, edtZipcode);
}
}
});
} catch (IOException exc) {
exc.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
答案 1 :(得分:0)
首先创建一个意图服务以获取位置地址。
<强>的AndroidManifest.xml 强>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.android.gms.location.sample.locationaddress" >
<application
...
<service
android:name=".FetchAddressIntentService"
android:exported="false"/>
</application>
...
然后使用 IntentService 扩展您的课程以使用onHandle
@Override
protected void onHandleIntent(Intent intent) {
String errorMessage = "";
// Get the location passed to this service through an extra.
Location location = intent.getParcelableExtra(
Constants.LOCATION_DATA_EXTRA);
...
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(),
// In this sample, get just a single address.
1);
} catch (IOException ioException) {
// Catch network or other I/O problems.
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " +
"Latitude = " + location.getLatitude() +
", Longitude = " +
location.getLongitude(), illegalArgumentException);
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
// Fetch the address lines using getAddressLine,
// join them, and send them to the thread.
for(int i = 0; i < address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
Log.i(TAG, getString(R.string.address_found));
deliverResultToReceiver(Constants.SUCCESS_RESULT,
TextUtils.join(System.getProperty("line.separator"),
addressFragments));
}
}
如需更多说明,您也可以使用以下链接