1

我按照本教程 获取 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 获取地址..这里需要帮助....如果还有其他方法可以获取地图的中心点和同一点的地址...任何形式的帮助将不胜感激...谢谢

4

2 回答 2

0
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";
        }
于 2015-06-01T08:51:53.097 回答
0

我想,你想将 LatLang 作为参数传递。我用以下代码解决了这个问题:

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;
    }

希望这对您有用>>>

编辑后:

您可以遵循这一点,如果您正在使用 Activity 类,如果您正在使用 Fragment 类,那么以下代码将适用于您:

new ReverseGeoCoding(getActivity()).execute(centerFromPoint);
于 2015-06-01T11:28:25.663 回答