get City from Location - Android Map

Android examples for Map:City Location

Description

get City from Location

Demo Code


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

public class Main {
    public static String getCity(Context context, Location location) {
        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        double latitude = location.getLatitude();
        double longitude = location.getLongitude();
        List<Address> addresses = null;
        try {/*from   ww  w.j a va 2 s . c  o  m*/
            addresses = geocoder.getFromLocation(latitude, longitude, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return addresses.get(0).getLocality();
    }
}

Related Tutorials