将Lat Long转换为Google Maps ApiV2

Gau*_*ora 2 android google-maps-android-api-2

我正在使用Google Maps Api V2制作演示应用.

我添加了一个简单的标记,并使其可拖动

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.demo_v2);


 googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
 googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
 googleMap.setOnMapClickListener(this);
 googleMap.setOnMarkerDragListener(this);
 googleMap.setOnMapLongClickListener(this);



Run Code Online (Sandbox Code Playgroud)

}

现在我希望当用户点击标记时地址出现.

简单的问题是:想要在API v2中从Lat Long获取地址(名称)

Amo*_*uli 14

试试这个:

public List<Address> getAddress() {
    if (latitude != 0 && longitude != 0) {
        try {
            Geocoder geocoder = new Geocoder(context);
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            String address = addresses.get(0).getAddressLine(0);
            String city = addresses.get(0).getAddressLine(1);
            String country = addresses.get(0).getAddressLine(2);
            Log.d("TAG", "address = " + address + ", city = " + city + ", country = " + country);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        Toast.makeText(context, "latitude and longitude are null", Toast.LENGTH_LONG).show();
    }
    return addresses;
}
Run Code Online (Sandbox Code Playgroud)