简体   繁体   中英

Get location name from lat and long without internet/wifi

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();
                }

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM