Skip to content
Snippets Groups Projects
Select Git revision
  • 9d608af678c375c7e0bcb9c617d4f0196ea9a503
  • master default
2 results

TrackGps.java

Blame
  • LocationAdress.java 2.68 KiB
    package com.example.srinivasan.database2;
    
    import android.content.Context;
    import android.location.Address;
    import android.location.Geocoder;
    import android.os.Bundle;
    import android.os.Message;
    import android.util.Log;
    
    import java.io.IOException;
    import java.util.List;
    import java.util.Locale;
    import java.util.logging.Handler;
    
    /**
     * Created by SRINIVASAN on 4/22/2017.
     */
    
    public class LocationAdress {
        private static final String TAG = "LocationAddress";
    
        public static String getAddressFromLocation(final double latitude, final double longitude,
                                                  final Context context)
        {
                    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                    String result = null;
                    try {
                        List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1);
                        if (addressList != null && addressList.size() > 0) {
                            Address address = addressList.get(0);
                            StringBuilder sb = new StringBuilder();
                            for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                                sb.append(address.getAddressLine(i)).append("\n");
                            }
                            sb.append(address.getLocality()).append("\n");
                            sb.append(address.getPostalCode()).append("\n");
                            sb.append(address.getCountryName());
                            result = sb.toString();
                            return result;
                        }
                    } catch (IOException e) {
                        Log.e(TAG, "Unable connect to Geocoder", e);
                    }/* finally {
                        Message message = Message.obtain();
                        message.setTarget(handler);
                        if (result != null) {
                            message.what = 1;
                            Bundle bundle = new Bundle();
                            result = "Latitude: " + latitude + " Longitude: " + longitude +
                                    "\n\nAddress:\n" + result;
                            bundle.putString("address", result);
                            message.setData(bundle);
                        } else {
                            message.what = 1;
                            Bundle bundle = new Bundle();
                            result = "Latitude: " + latitude + " Longitude: " + longitude +
                                    "\n Unable to get address for this lat-long.";
                            bundle.putString("address", result);
                            message.setData(bundle);
                        }
                        message.sendToTarget();
                    }*/
                    return result;
        }
    }