如何获取android中GoogleMap v2的中心点地址?

how to get the address of center point of GoogleMap v2 in android?

我按照 this tutorial 获取 v2 GoogleMap 中心点的位置。现在我想使用反向地理编码获取该点的地址....

我正在使用以下代码在单击按钮时获取中心点的 LatLang:

   @Override
public void onClick(View view) {
    if(view.getId()==R.id.btn_set_location){

        VisibleRegion visibleRegion = map.getProjection()
                .getVisibleRegion();

        Point x = map.getProjection().toScreenLocation(
                visibleRegion.farRight);

        Point y = map.getProjection().toScreenLocation(
                visibleRegion.nearLeft);

        Point centerPoint = new Point(x.x / 2, y.y / 2);

        LatLng centerFromPoint = map.getProjection().fromScreenLocation(
                centerPoint);



    }
}

我看过其他教程分别使用纬度和经度获取地址,在我的例子中,我有 LatLang,如何使用 LatLang 获取地址……这里需要帮助……如果还有其他获取中心的方法地图点和同一点的地址...任何类型的帮助将不胜感激...谢谢

public class AsyncRouteGetter extends AsyncTask<Double, String, String> {
protected String doInBackground(Double... coords) {
    String xml = "";
    String url = "http://maps.google.com/maps/api/geocode/xml?address=" + coords[0] + "," + coords[1] + "&sensor=false";
    //String url = "http://maps.google.com/maps/api/geocode/json?address=51.0031761,17.0499418&sensor=false";
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);
    } catch (Exception e) { e.printStackTrace(); }
    //Log.d("LOK", xml);

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    Document doc = null;
    try {
        builder = factory.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(xml));
        doc = builder.parse(is);
    } catch (Exception e) { Log.d("DOC", "BLAD2"); e.printStackTrace(); return null; }
    String result = doc.getElementsByTagName("formatted_address").item(0).getTextContent();
    Log.d("LOKALIZACJA", result);

    return result;
    }
}

这非常适合我。请随时将其标记为答案:)

更新

 AsyncRouteGetter arg = new AsyncRouteGetter();
        arg.execute(location.latitude, location.longitude);
        String route = null;
        try {
            route = arg.get();
        } catch (Exception e) { Log.d("LOK", "Error1"); }
        if (route != null) {
            Log.d("LOK", route);
            currentAddress = route;
        } else {
            Log.d("LOK", "Error2");
            currentAddress = "anything, so you wont work with null";
        }

我认为,您希望将 LatLang 传递为 parameters.I 使用以下代码解决了问题:

private class ReverseGeoCoding extends AsyncTask<LatLng, Void, String> {
    Context mContext;


    public ReverseGeoCoding(Context context){
        super();
        mContext = context;
    }

    // Finding address using reverse geocoding
    @Override
    protected String doInBackground(LatLng... params) {
        Geocoder geocoder = new Geocoder(mContext);
        double latitude = params[0].latitude;
        double longitude = params[0].longitude;

        List<Address> addresses = null;
        String addressText="";

        try {
            addresses = geocoder.getFromLocation(latitude, longitude,1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(addresses != null && addresses.size() > 0 ){
            Address address = addresses.get(0);

            addressText = String.format("%s, %s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                    address.getLocality(),
                    address.getCountryName());
        }

        return addressText;
    }

希望这对您有用>>>

编辑后:

您可以关注 this 如果您正在处理 Activity Class 如果您正在处理 Fragment class 那么以下代码将适用于您:

new ReverseGeoCoding(getActivity()).execute(centerFromPoint);