get Address Location from Context - Android Map

Android examples for Map:Address

Description

get Address Location from Context

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;
import android.location.LocationManager;
import android.util.Log;

public class Main {
    private static LocationManager locManager;
    private static Location loc;
    private static Geocoder geo;

    public static String getAddress(Context context) {
        init(context);//from  ww  w .  ja  v a2s .  c o m
        double latitude = loc.getLatitude();
        double longitude = loc.getLongitude();
        StringBuffer temp = new StringBuffer();
        try {
            List<Address> list = geo.getFromLocation(latitude, longitude,
                    10);
            if (list.size() > 0) {
                for (Address addr : list) {
                    temp.append(addr.getAddressLine(0));
                }
            }
        } catch (IOException e) {
            Log.e("error", "getAddress");
        }
        return temp.toString();
    }

    private static void init(Context context) {
        locManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        loc = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        loc = loc == null ? locManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
                : loc;
        geo = new Geocoder(context, Locale.getDefault());
    }
}

Related Tutorials