Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

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 getAddressFromLoc(Context pContext, double latitude, double longitude) throws IOException {

        Geocoder geocoder = new Geocoder(pContext, Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);

        String address = "";
        if (addresses.get(0) != null)
            address = addresses.get(0).getAddressLine(0);

        String city = addresses.get(0).getLocality();
        String zip = addresses.get(0).getPostalCode();
        String country = addresses.get(0).getCountryName();

        address = (address == null) ? "" : address;
        city = (city == null) ? "" : city;
        zip = (zip == null) ? "" : zip;
        country = (country == null) ? "" : country;

        return address + " " + city + " " + zip + " " + country;
    }
}