Search code examples
androidgoogle-mapsgps

Given a latitude and longitude, Get the location name


I am developing an application wherein I get the latitude, longitude in my android device and post them to a web server.

and in a web application I show the location details with the help of google maps.

Here I have huge collection of lat long values which i loop it in my jsp and create multiple markers with info windows.

Now the problem is that I need to show the location name of the particular latitude and longitude in the Info Window of google maps.

Can anyone help me with the google maps script or how can i get the location name from the lat long values in my android phone itself, so that I can post that to my web server.


Solution

  • Here i am given a single just pass the latitude and longitude in this function then you got all the information related to this latitude and longitude.

    public void getAddress(double lat, double lng) {
        Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
            Address obj = addresses.get(0);
            String add = obj.getAddressLine(0);
            add = add + "\n" + obj.getCountryName();
            add = add + "\n" + obj.getCountryCode();
            add = add + "\n" + obj.getAdminArea();
            add = add + "\n" + obj.getPostalCode();
            add = add + "\n" + obj.getSubAdminArea();
            add = add + "\n" + obj.getLocality();
            add = add + "\n" + obj.getSubThoroughfare();
    
            Log.v("IGA", "Address" + add);
            // Toast.makeText(this, "Address=>" + add,
            // Toast.LENGTH_SHORT).show();
    
            // TennisAppActivity.showDialog(add);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }