pallavi
pallavi

Reputation: 171

How to get city name ontap on the map in google map android v2?

By tapping on the map, How can I get the address of that location. On tap at particular location we can able listen and pass those co-ordinates we can get address but it should make work in android Google map v2.

Upvotes: 4

Views: 3573

Answers (3)

adek
adek

Reputation: 3121

If you have your LatLng data - it's quite easy. You need to use Goecoder.

    protected String doInBackground(Location... params) {
        Geocoder geocoder =
                new Geocoder(mContext, Locale.getDefault());
        // Get the current location from the input parameter list
        Location loc = params[0];
        // Create a list to contain the result address
        List<Address> addresses = null;
        try {
            /*
             * Return 1 address.
             */
            addresses = geocoder.getFromLocation(loc.getLatitude(),
                    loc.getLongitude(), 1);
        } catch (IOException e1) {
        Log.e("LocationSampleActivity",
                "IO Exception in getFromLocation()");
        e1.printStackTrace();
        return ("IO Exception trying to get address");
        } catch (IllegalArgumentException e2) {
        // Error message to post in the log
        String errorString = "Illegal arguments " +
                Double.toString(loc.getLatitude()) +
                " , " +
                Double.toString(loc.getLongitude()) +
                " passed to address service";
        Log.e("LocationSampleActivity", errorString);
        e2.printStackTrace();
        return errorString;
        }
        // If the reverse geocode returned an address
        if (addresses != null && addresses.size() > 0) {
            // Get the first address
            Address address = addresses.get(0);
            /*
             * Format the first line of address (if available),
             * city, and country name.
             */
            String addressText = String.format(
                    "%s, %s, %s",
                    // If there's a street address, add it
                    address.getMaxAddressLineIndex() > 0 ?
                            address.getAddressLine(0) : "",
                    // Locality is usually a city
                    address.getLocality(),
                    // The country of the address
                    address.getCountryName());
            // Return the text
            return addressText;
        } else {
            return "No address found";
        }
    }
    ...
}
...

}

The best way to do it is to use AsyncTask. You will find complete example at Android Devlopers' site: http://developer.android.com/training/location/display-address.html

Upvotes: 2

Hybrid Developer
Hybrid Developer

Reputation: 2340

This may help you to use the complete map opertion

Upvotes: 2

Rarw
Rarw

Reputation: 7663

You can do this with the android GeoCoder. With GoogleMaps v2 you can use longpress to get a LatLng point. By taking those coordinates and passing them to the GeoCoder you can "reverse geocode" to find the address.

Upvotes: 1

Related Questions