Is it possible to get address from lat long from only GPS while my wi-fi and mobile data is off?

828 views Asked by At

I am testing an app which takes latitude and longitude with help of GPS in Samsung Tablet. I am able to capture current latitude, longitude from GPS unit. Now I want to get address from these location using latitude longitude without use of Internet i.e. wi-fi, mobile data.

I am doing it below manner:

public  String getAddress(Context ctx, double latitude, double longitude) {
        StringBuilder result = new StringBuilder();
        try {
            Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
            List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);

                String locality=address.getLocality();
                String city=address.getCountryName();
                String region_code=address.getCountryCode();
                String zipcode=address.getPostalCode();
                double lat =address.getLatitude();
                double lon= address.getLongitude();

                result.append(locality+" ");
                result.append(city+" "+ region_code+" ");
                result.append(zipcode);

            }
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }

        return result.toString();
    }

    private class GetCurrentAddress extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... urls) {
    // this lat and log we can get from current location but here we given hard coded
            double latitude=12.916523125961666;
            double longitude=77.61959824603072;

        String address= getAddress(context, latitude, longitude);
            return address;
        }

        @Override
        protected void onPostExecute(String resultString) {
            dialog.dismiss();
            result.setText(resultString);

        }
    }  

In mainfest i do below entries:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3

There are 3 answers

0
Deepak Gupta On BEST ANSWER

It is not possible to get Address from Lat Lang without internet because google also do geocoding

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

this getFromLocation() method should be run in asynTask and with internet then it gives desired result.

    <uses-permission android:name="android.permission.INTERNET" />

Internet permission also need

1
AndroidHacker On

Yes it is possible if U just need to get lat/long of user using GPS.

When any case for Geo-coding comes into consideration then you will need network connection in order to hit Google API for same.

Hope it fulfills your query !

0
Satyaki Mukherjee On

It's not possible because GeoCoder needs external service which depends on internet.