Reverse GeoCoordinate Class gives Location Not found error

583 views Asked by At

i m using google Reverse Geocoding API in my app, i m succussfully able to get get coordinate using google geolocation API. Now i m trying to get Location Name from Reverse Geocoding API , but always returns Location not found error

here is my code:

  Geocoder geocoder= new Geocoder(MainActivity.this, Locale.ENGLISH);
    

  if(geocoder.isPresent()){
          List<Address> list;
 try {
   list = geocoder.getFromLocation(37.42279, -122.08506,1);
    Address address = list.get(0);
    Log.d("this is working","thsi sis working");

          StringBuffer str = new StringBuffer();
          str.append("Name: " + address.getLocality() + "\n");
          str.append("Sub-Admin Ares: " + address.getSubAdminArea() + "\n");
          str.append("Admin Area: " + address.getAdminArea() + "\n");
          str.append("Country: " + address.getCountryName() + "\n");
          str.append("Country Code: " + address.getCountryCode() + "\n");;

          String strAddress = str.toString();
          JsonDatas.setText(strAddress);
           Log.d("Address", strAddress);
  } 
    catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

i have several question

  1. how google know that which app requested the API and how google determine of Quota that app. in google API developer Console, google gave Quota for app API
  2. Why i m getting location not found error but searching on google map location is showing
  3. do i need to add google Geocoder APi key into my app - right now i m only Geolocation API key using for retrieve Coordinates

Please correct me if i m wrong, Please give suggestion so my code will work fine

thanks

1

There are 1 answers

0
Shvet On BEST ANSWER

Make sure you have latest Google play library in your project. I am using this code and working for me.

 private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String> {
        Context mContext;

        public ReverseGeocodingTask(Context context) {
            super();
            mContext = context;
        }

        // Finding address using reverse geocoding
        @Override
        protected String doInBackground(LatLng... params) {
            Geocoder geocoder = new Geocoder(mContext);
            double latitude = params[0].latitude;
            double longitude = params[0].longitude;
            List<Address> addresses = null;
            String addressText = "";

            try {

                addresses = geocoder.getFromLocation(latitude, longitude, 1);

                Thread.sleep(500);

                if (addresses != null && addresses.size() > 0) {

                    Address address = addresses.get(0);

                    addressText = String.format(
                            "%s",
                            address.getMaxAddressLineIndex() > 0 ? address
                                    .getAddressLine(1) : "", "", "");
                }
            } catch (IOException e) {
                e.printStackTrace();

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return addressText;
        }

        @Override
        protected void onPostExecute(String addressText) {
        }
}

Where LatLng is Latitude and longitute, check this for doc.

and to use write down new ReverseGeocodingTask(this).execute(latlng); where you want to get data.

Make sure you are adding permission into your manifest file.

 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />