get Address via Geocoder - Android Map

Android examples for Map:Address

Description

get Address via Geocoder

Demo Code


//package com.java2s;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class Main {
    public static String getAddress(Context context, double latitudes,
            double longitudes) {
        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        String location = null;//from   www  . ja  v a2s.  c  o m
        double latitude = latitudes;
        double longitude = longitudes;
        List<Address> addresses = null;
        StringBuilder coordinates = new StringBuilder();
        try {
            addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses != null && addresses.size() > 0) {
                Address fullAddress = addresses.get(0);
                String address = fullAddress.getAddressLine(0);
                String city = fullAddress.getAddressLine(1);
                String country = fullAddress.getAddressLine(2);

                if (address != null) {
                    coordinates.append(address).append("\n");
                }

                if (city != null) {
                    coordinates.append(city).append("\n");
                }

                if (country != null) {
                    coordinates.append(country);
                }
                location = coordinates.toString();
            } else {
                location = (float) latitude + " " + (float) longitude;
            }
        } catch (IOException e) {
            location = latitude + " " + longitude;
            e.printStackTrace();
        }
        return location;
    }
}

Related Tutorials