Convert Location To City Country - Android Map

Android examples for Map:City Location

Description

Convert Location To City Country

Demo Code


//package com.java2s;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.Context;
import android.location.Address;

import android.location.Geocoder;
import android.location.Location;

public class Main {
    public static String ConvertLocationToCityCountry(Context context,
            Location location) {/*w w w . j  a v  a  2  s  .  c  om*/
        int timeout = 10;
        StringBuffer address = new StringBuffer();
        Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
        try {
            List<Address> addresses = geoCoder.getFromLocation(
                    location.getLatitude(), location.getLongitude(), 10);

            int loop = 0;
            while (loop < timeout) {
                addresses = geoCoder
                        .getFromLocation(location.getLatitude(),
                                location.getLongitude(), 10);
                loop++;
                if (addresses.size() > 0) {
                    break;
                }
            }

            if (addresses.size() > 0) {
                if (addresses.get(0).getAdminArea() != null) {
                    address.append(addresses.get(0).getAdminArea());
                }
                if (addresses.get(0).getCountryName() != null) {
                    address.append(", ");
                    address.append(addresses.get(0).getCountryName());
                }

            }
        } catch (IOException e) {
            return "Nearby";
        }

        return address.toString();
    }
}

Related Tutorials