Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

java - Geocoder doesn't always return a value

I am able to successfully get lat/long and pass it to the geocoder to get an Address. However, I don't always get an address back. Seems like it takes a couple of attempts? I'm not sure why.

Is there a better way for me to obtain the address at this point?

public List<Address> getAddresses(){
          Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
          List<Address> addresses = new ArrayList();
             try {
       addresses = geocoder.getFromLocation(latitude, longitude, 1);
      } catch (IOException e) {
       e.printStackTrace();
      }

      return addresses;
        }

I am calling this method here:

LocationListener onLocationChange=new LocationListener() {
        public void onLocationChanged(Location loc) {
            //sets and displays the lat/long when a location is provided
            String latlong = "Lat: " + loc.getLatitude() + " Long: " + loc.getLongitude();   
            latitude = loc.getLatitude();
            longitude = loc.getLongitude();

            List<Address> addresses = getAddresses();

            if(addresses.size() > 0 && addresses != null)
             zipcode = addresses.get(0).getPostalCode();

            Toast.makeText(getApplicationContext(), zipcode,Toast.LENGTH_SHORT).show();
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In my experience, Googles Geocoder doesn't always work, I have a couple of fixed points on the map, when I click on the overlay it pops a toast with the address for that lat/long, these points do not change, sometimes I click on a same point 10 times, but I only get an address 7 of them.

It's weird, but thats the way it is, I just modified my app to work around the problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...