Getting zip code without Geocoder

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Getting zip code without Geocoder

Postby Adamah » Sun Oct 24, 2010 8:31 pm

Hello everyone. I'm working on a simple weather application that requires the user's general location. I don't need it to be that accurate - the nearest cell tower would do. In testing, I can get the user's GPS coordinates just fine, but Geocoder often does not return any associated addresses. The success rate is about 50%, and sometimes it takes 10+ requests (of the same coordinates) before Geocoder returns anything. Is it possible to get a zip code without using Geocoder, or is there something else I can do to improve my success rate? I appreciate any help!

Here is the relevant code:

Code: Select all
            LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            String locationProvider = LocationManager.NETWORK_PROVIDER;
            Location lastLoc = locationManager.getLastKnownLocation(locationProvider);
            double lat = lastLoc.getLatitude();
            double lon = lastLoc.getLongitude();
            Geocoder gc = new Geocoder(this, Locale.getDefault());
            String zip = "";
            List <Address> addresses = gc.getFromLocation(lat, lon, 1);
            if (addresses.size() > 0) {
               zip = addresses.get(0).getPostalCode();
            }
            int count = 0;
            while (zip.equals("") && count < 15) {
               count++;
               Thread.sleep(1000*count);
               addresses = gc.getFromLocation(lat, lon, 1);
               if (addresses.size() > 0) {
                  zip = addresses.get(0).getPostalCode();
               }
            }
Adamah
Freshman
Freshman
 
Posts: 3
Joined: Sun Oct 24, 2010 8:25 pm

Top

Return to Map Problems

Who is online

Users browsing this forum: Exabot [Bot] and 6 guests