我有经度和纬度坐标,我想从中得到村庄名称区和子区。我搜索并找到没有给出村庄名称的反向地理编码。
是否有任何api或代码库可用于从经纬度获取确切位置。我想在我的Android应用程序中显示它。
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
String result = null;
try {
List<Address> addressList = geocoder.getFromLocation(
25.882456,83.174279, 1);
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append("\n");//adress
}
sb.append(address.getLocality()).append("\n");//village
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
sb.append(address.getAdminArea()).append("\n"); //state
sb.append(address.getSubAdminArea()).append("\n");//district
result = sb.toString();
}
} catch (IOException e) {
// Log.e(TAG, "Unable connect to Geocoder", e);
}
最佳答案
我在您的代码中更改了,它正在工作
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.app.AlertDialog;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
private GoogleMap mMap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap != null) {
return;
}
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (mMap == null) {
return;
}
// Initialize map options. For example:
// mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
final LatLng INDIA = new LatLng(28.5192, 77.2130);
// String address=getAddress(this, 28.5192, 77.2130);
Marker melbourne = mMap.addMarker(new MarkerOptions()
.position(INDIA)
.title("Saket")
.snippet("Population: 4,137,400"));
// String address=getAddress(this,28.6700, 77.4200);
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
String result = null;
try {
List<Address> addressList = geocoder.getFromLocation(
25.882456,83.174279, 1);
if (addressList != null && addressList.size() > 0) {
Address address = addressList.get(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append("\n");//adress
}
sb.append(address.getLocality()).append("\n");//village
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
sb.append(address.getAdminArea()).append("\n"); //state
sb.append(address.getSubAdminArea()).append("\n");//district
sb.append(address.getSubLocality()).append("\n");
result = sb.toString();
}
} catch (IOException e) {
// Log.e(TAG, "Unable connect to Geocoder", e);
}
new AlertDialog.Builder(this).setMessage(result).create().show();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
final LatLng Ghaziabad = new LatLng(24.1860076, 75.1432231);
Marker ghaziabad = mMap.addMarker(new MarkerOptions()
.position(Ghaziabad)
.title(result)
.snippet("Population: 4,137,400")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
}