0

I'm just trying to get the country name in my Map Activity when I touch the map. I've followed Android: Reverse geocoding - getFromLocation and here's my code in onTouchEvent:

public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    {   
        //---when user lifts his finger---

        if (event.getAction() == 1) {                
            GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());

            Geocoder geoCoder = new Geocoder(mContext, Locale.getDefault());
            try {
                List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6()/1E6, p.getLongitudeE6()/1E6, 1);

                String add = "";
                if (addresses.size() > 0) 
                {
                    for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
                    {
                        add += addresses.get(0).getAddressLine(i) + "\n";
                        add += addresses.get(0).getCountryName() + "\n";
                    }
                }

                Toast.makeText(mContext, add, Toast.LENGTH_LONG).show();
            }
            catch (IOException e) {                
                e.printStackTrace();
            }   
            return false;
        }
        else                
            return false;
    } 

All I want is that the Toast will show the address and country name when the user lifts his finger. When the application runs, it doesn't show any error but also doesn't show the Toast. Logcat shows java.io.IOException: Service not Available.

At first I thought there's an error in my code inside onTouchEvent, so I made an experiment to change it into this, to check if the Toast is shown.

public boolean onTouchEvent(MotionEvent event, MapView mapView) {
        // TODO Auto-generated method stub
        if (event.getAction() == 1) {
            GeoPoint p = mapView.getProjection().fromPixels((int)event.getX(), (int)event.getY());
            Toast.makeText(mContext, p.getLatitudeE6() / 1E6 + ", " + p.getLongitudeE6() / 1E6, Toast.LENGTH_SHORT).show();
        }
        return false;
    }

The Toast shows the latitude and longitude perfectly so I still wonder what's wrong when I just want to get the country name. And I still don't have any idea about the logcat that says: Service Not Available. Is there any limitation when I want to do reverse geocoding?

Community
  • 1
  • 1
Rommy Saiko
  • 43
  • 1
  • 1
  • 6

1 Answers1

0

I have an alternative approach where you can get the complete address of the lat and lng of a specific point.Just pass your lat and lng to the below Google maps URL and will return the exact address of that particular point in XML format.

http://maps.google.com/maps/api/geocode/xml?address=

Pavandroid
  • 1,586
  • 2
  • 15
  • 30