Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.IOException;

import java.util.List;

import android.content.Context;

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

import android.util.Log;

public class Main {
    private static final String LogDavid = "LogDavid";
    private static Geocoder geocoder;

    public static String locationToAddress(Location loc, Context context) {
        try {
            double latitude, longitude;
            String addressText = "";
            geocoder = new Geocoder(context);
            latitude = loc.getLatitude();
            longitude = loc.getLongitude();
            List<Address> list = geocoder.getFromLocation(latitude, longitude, 1);

            if (list != null && list.size() > 0) {
                Address address = list.get(0);
                addressText = String.format("%s, %s, %s",
                        address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                        address.getLocality(), address.getCountryName());
            }

            return addressText;
        } catch (IOException e) {
            Log.e(LogDavid, "ERROR:" + e.toString());
            return "";
        }
    }
}