0

I am working on an app which requires City and street name from the obtained GPS coordinates, I already tried geocoder but it is only working on internet. But i want to get location name or city name even when i dont have internet connection. I am unable to resolve the issue below is the code i am using

if (gps.canGetLocation()) {
                latitude = gps.getLatitude();
                longitude = gps.getLongitude();
                Toast.makeText(
                        getApplicationContext(),
                        "Lat = " + String.valueOf(latitude) + "\nLong : "
                                + String.valueOf(longitude),
                        Toast.LENGTH_LONG).show();

                Geocoder geocoder = new Geocoder(MainActivity.this, Locale
                        .getDefault());
                try {
                    addresses = geocoder.getFromLocation(latitude,
                            longitude, 1);
                } catch (IOException e) { // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (addresses.size() > 0) {
                    String cityName = addresses.get(0).getAddressLine(0);
                    String stateName = addresses.get(0).getAddressLine(1);
                    String countryName = addresses.get(0).getAddressLine(2);
                    Toast.makeText(
                            getApplicationContext(),
                            "Area = " + cityName + "\nCity = " + stateName
                                    + "\nCountry Name = " + countryName,
                            Toast.LENGTH_LONG).show();
                }

                //
                if (latitude > 0 && longitude > 0) {
                    String[] a = { load("C1.txt"), load("C2.txt"),
                            load("C2.txt") };

                    for (int j = 0; j < a.length; j++) {
                        if (a[j].length() > 10) {
                            SmsManager sms = SmsManager.getDefault();
                            /*
                             * sms.sendTextMessage(a[j], null, "Area = " +
                             * cityName + "\nCity = " + stateName +
                             * "\nCountry Name = " + countryName, null,
                             * null);
                             */
                            sms.sendTextMessage(
                                    a[j],
                                    null,
                                    "Lat = " + String.valueOf(latitude)
                                            + "\nLong : "
                                            + String.valueOf(longitude),
                                    null, null);
                        }
                    }

                } else {
                    Toast.makeText(getApplicationContext(),
                            "Please enter phone numbers from settings",
                            Toast.LENGTH_LONG).show();
                }
farhan678
  • 85
  • 13
  • http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-a/3145655#3145655 – Vishal Gaur Nov 19 '15 at 16:07
  • thanks for the link but i need the location name. It returns lat n longs which i can get from gps also – farhan678 Nov 19 '15 at 16:29
  • I need name of current city street adress etc .. like geocoder – farhan678 Nov 19 '15 at 16:29
  • 1
    How do you expect this to work without the internet. Are you going to ship a huge static database for all of the world with your app? What sort of level of resolution is acceptable? – Ifor Nov 19 '15 at 16:49
  • yeah thats the problem.... so it cant be done offline ... Thanks :) – farhan678 Nov 20 '15 at 16:54
  • I need coordinates for a 100km specific area. So last option is now to get coordinates of required places n apply check on lat n longs – farhan678 Nov 20 '15 at 16:55

1 Answers1

2

This is not working without internet as the geocoder (or whatever you want to use) has to retrieve the information from some kind of data source. As long as you do not want to store an entire database in your app (which wont be possible because of the small storage sizes on most smartphones) you'll have to retrieve the data from a server.

crysxd
  • 3,177
  • 20
  • 32