get Geo Code for Map Location - Android Map

Android examples for Map:Google Map

Description

get Geo Code for Map Location

Demo Code


//package com.java2s;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.support.annotation.NonNull;
import android.util.Log;
import java.io.IOException;
import java.util.List;

public class Main {
    private static List<Address> geocodeMatches = null;

    public static List<Address> getGeoCode(@NonNull Context context,
            @NonNull String address) {
        try {/*from  ww  w.  ja v  a 2 s . c  om*/
            geocodeMatches = new Geocoder(context).getFromLocationName(
                    address, 1);

            if (!geocodeMatches.isEmpty()) {
                double latitude = geocodeMatches.get(0).getLatitude();
                double longitude = geocodeMatches.get(0).getLongitude();
                Log.d("latitude: ", latitude + "");
                Log.d("longitude", longitude + "");

                return geocodeMatches;
            }

        } catch (IOException e) {
            Log.e("ERROR", "Error to retrieve geocode" + e);
            e.printStackTrace();
        }
        return geocodeMatches;
    }
}

Related Tutorials