Android Context Get getCurrentLocationByName( Context context)

Here you can find the source of getCurrentLocationByName( Context context)

Description

public static HashMap getCurrentLocationByName(Context context)

This method will return the address of the location in the form of hash map in key value pair, Values of the address can be retrieved using keys.

License

Apache License

Declaration

public static HashMap<String, String> getCurrentLocationByName(
        Context context) 

Method Source Code

//package com.java2s;
/*/*from  w w  w .  ja  v a  2  s  . c  o  m*/
 * Copyright 2013 Nagendra K Srivastava.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;
import java.util.HashMap;
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;

public class Main {
    /**
     * <b><i>public static HashMap<String,String> getCurrentLocationByName(<font color="red"><u>Context</u></font> context)</b></i></p>
     * This method will return the address of the location in the form of hash map in key value pair, Values of the address can be retrieved using keys.
     * The available keys are <font color = "red">country</font>, <font color = "red">admin_area</font>, <font color = "red">feature_name</font>, <font color = "red">locality_name</font>, 
     * <font color = "red">postal_code</font>, <font color = "red">premises</font>, <font color = "red">sub_adminarea</font>, <font color = "red">sub_locality</font>,
     *  <font color = "red">street_no</font>, <font color = "red">street_address</font>
     * 
     */
    public static HashMap<String, String> getCurrentLocationByName(
            Context context) {
        HashMap<String, String> addressValues = new HashMap<String, String>();
        double latitude = 0.0;
        double longtiude = 0.0;
        LocationManager locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        Location quickLocation = locationManager
                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (quickLocation != null) {
            latitude = quickLocation.getLatitude();
            longtiude = quickLocation.getLongitude();
        }
        Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
        try {
            List<Address> addresses = geoCoder.getFromLocation(latitude,
                    longtiude, 1);
            if (addresses.size() > 0) {
                addressValues.put("country", addresses.get(0)
                        .getCountryName());
                addressValues.put("admin_area", addresses.get(0)
                        .getAdminArea());
                addressValues.put("feature_name", addresses.get(0)
                        .getFeatureName());
                addressValues.put("locality_name", addresses.get(0)
                        .getLocality());
                addressValues.put("postal_code", addresses.get(0)
                        .getPostalCode());
                addressValues.put("premises", addresses.get(0)
                        .getPremises());
                addressValues.put("sub_adminarea", addresses.get(0)
                        .getSubAdminArea());
                addressValues.put("sub_locality", addresses.get(0)
                        .getSubLocality());
                addressValues.put("street_no", addresses.get(0)
                        .getSubThoroughfare());
                addressValues.put("street_address", addresses.get(0)
                        .getThoroughfare());

            }
        } catch (IOException e) {

            e.printStackTrace();
        }
        return addressValues;
    }
}

Related

  1. getCornerOffset(Context context)
  2. getCornerPaint(Context context)
  3. getCornerRadius(Context context)
  4. getCurProcess(Context context)
  5. getCurrentHome(Context c)
  6. getCurrentSsid(Context context)
  7. getDataFile(Context context, String fileName)
  8. getDefaultDisplayMetrics( @Nonnull Context context)
  9. getDefaultDisplayMetrics( @Nonnull Context context)