1

I following this tutorial to get position of center point of v2 GoogleMap .now I want to get the address of the point using reverse geocoding ....

I am using Following code to get LatLang of center point when button is clicked :

   @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);



    }
}

I have seen others tutotials to get address using latitude and longitude seperately in my case I have got LatLang ,how to get the address using LatLang..need help here ....if there are others ways to get center point of map and the address of the same point ...any kind of help would be appreciated ...thank you

Community
  • 1
  • 1
Santosh Bhandary
  • 359
  • 2
  • 3
  • 19

2 Answers2

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

this works perfectly for me. Feel free to mark this as an answer :)

UPDATE

 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";
        }
xenteros
  • 15,586
  • 12
  • 56
  • 91
  • I throughly studied your code @xenteros ,so how do I call AsyncRouteGetter() from onclick method ....I am facing problem in passing parameters. – Santosh Bhandary Jun 01 '15 at 09:50
  • I have already got latitude and longitude in "centerFromPoint " only thing i need to do is pass it as parameters @xenteros – Santosh Bhandary Jun 01 '15 at 11:43
  • In this 2nd code replace location.latitude with first coordinate and location.longitude with second one. Then in String currentAdress you will have adress like street or building name. Is it what you want? – xenteros Jun 01 '15 at 15:51
0

I think, you want to pass LatLang as parameters.I solved the problem with following code:

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

Hope this will work ourt for you>>>

After editing:

You can follow this and if you are working on Activity Class if you working on Fragment class then the following codes will work for you:

new ReverseGeoCoding(getActivity()).execute(centerFromPoint);
BugMaker
  • 1
  • 5
  • so how do I call AsyncTask from my button event handling section I am having problem in calling class with appropiate parameters ...I am following above answer but still not working in case of calling the class .... – Santosh Bhandary Jun 01 '15 at 11:33
  • thanks for the answer I went through out the link ...Since I am working in Activity class It worked out for me ....thanks for helping out – Santosh Bhandary Jun 01 '15 at 11:54