0

I want to get current location all information forexample country name, street i use Geo Encoder and use the code

Geocoder geocoder = new Geocoder(ctx, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);

The getFromLocation give service not found exception

Flat Eric
  • 7,971
  • 9
  • 36
  • 45
user3710617
  • 29
  • 1
  • 7
  • 1
    from documentation(did you even tried to read it?): `The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists` – Selvin Aug 06 '14 at 09:53
  • Are you using an emulator when you see this behaviour? – NickT Aug 06 '14 at 09:59
  • You must implement `LocationListener` first then in `onLocationChanged(Location)` try your code. – Ved Aug 06 '14 at 10:12
  • No i Use samsung mobile – user3710617 Aug 06 '14 at 10:12
  • Plz selvin send me the whole code i will check it out – user3710617 Aug 06 '14 at 10:13

2 Answers2

0

Try this

public class CityActivity extends Activity implements LocationListener{
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
TextView txtLat;
String lat;
String provider;
protected String latitude,longitude; 
protected boolean gps_enabled,network_enabled;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_city);
txtLat = (TextView) findViewById(R.id.textview1);

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000*60*5,100,this);
}
@Override
public void onLocationChanged(Location location) {
txtLat = (TextView) findViewById(R.id.textview1);
txtLat.setText("Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
 List<Address> addresses;
try {
    addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
    if (addresses.isEmpty()) {
        Log.d("CityActivity","Waiting for Location");
    }else{
    String CityName = addresses.get(0).getAddressLine(2);
     String StateName = addresses.get(0).getAddressLine(1);
     String CountryName = addresses.get(0).getAddressLine(0);
     String area=addresses.get(0).getLocality();
     Log.d("CityActivity", area);

    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    Log.e("CityActivity", "Error in try");
    e.printStackTrace();
}


}

@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}

@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}    
}
Ved
  • 1,035
  • 14
  • 28
0

I had exactly the same problem in my app and ended up writing my own GeocodeTask:

class GoogleApiGeocodingTask extends AsyncTask<Location, Void, JSONObject>{

  Locale defaultLoc = Locale.getDefault();

  GoogleApiGeocodingTask() {}

  @Override
  protected JSONObject doInBackground( Location... params ) {
    try{
      Location loc = params[ 0 ]
      ResponseTuple rt = doGet( "http://maps.google.com/maps/api/geocode/json?sensor=true&latLng=" + loc.getLattitude() + ',' + loc.getLongitude() + "&language=" + defaultLoc.getLanguage() );
      if( 200 == rt.getStatusCode() ) return rt.getJson();
    }catch( Exception e ){
      Log.e( "DelayedGeocodeHandler", "", e );
    }

    return null;
  }

  @Override
  protected void onPostExecute( JSONObject json ) {
    if( null == json || !"OK".equals( json.optString( "status" ) ) ) return;

    try{
      JSONArray array = json.getJSONArray( "results" ); 
      for( int ix = 0; ix < array.length(); ix++ ){
        JSONObject obj = array.optJSONObject( ix );
        JSONObject loc = obj.getJSONObject( "geometry" ).getJSONObject( "location" );
        JSONArray components = obj.getJSONArray( "address_components" );
        String country = null, city = null;
        for( int ixx = 0; ixx < components.length(); ixx++ ){
          JSONObject comp = (JSONObject)components.get( ixx );
          doSomethingWithAddressComponent( comp );
        }
      }
    }catch( Exception e ){
      Log.e( "GoogleApiGeocodingTask", "", e );
    }
  }
}

Util and ResponseTuple are my internal classes and I think, it's pretty obvious what they do

UPDATE:

the missing methods:

public ResponseTuple doGet( String url) throws Exception {
  HttpClient httpClient = getHttpClient();
  HttpConnectionParams.setConnectionTimeout( httpClient.getParams(), timeout );
  HttpGet httpget = new HttpGet( url );
  HttpResponse hr = httpClient.execute( httpget );
  return new ResponseTuple( hr.getStatusLine().getStatusCode(), asString( hr ) );
}

public static String asString( HttpResponse response ) throws Exception {
  BufferedReader reader = new BufferedReader( new InputStreamReader( response.getEntity().getContent() ) );
  try{
    StringBuilder sb = new StringBuilder();
    String line = null;
    while( null != ( line = reader.readLine() ) ) sb.append( line );
    return sb.toString().trim();
  }finally{
    reader.close();
  }
}

and the class:

public class ResponseTuple {

  private int statusCode;

  private String body;

  public ResponseTuple( int statusCode, String body ) {
    this.statusCode = statusCode;
    this.body = body;
  }

  public int getStatusCode() {
    return statusCode;
  }

  public String getBody() {
    return body;
  }

  public JSONObject getJson() throws JSONException {
    return new JSONObject( body );
  }

}

Also I saw, that you need a reverse geocodig, so I adopted the URL appropriately

injecteer
  • 20,038
  • 4
  • 45
  • 89