【问题标题】:GeoEncoder give Exception service not foundGeoEncoder 给 Exception service not found
【发布时间】:2014-09-29 04:41:50
【问题描述】:

我想获取当前位置的所有信息,例如国家名称、街道我使用地理编码器并使用代码

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

getFromLocationservice not found exception

【问题讨论】:

  • 来自文档(您是否尝试过阅读它?):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
  • 当您看到这种行为时,您是否正在使用模拟器?
  • 您必须先实现LocationListener,然后在onLocationChanged(Location) 中尝试您的代码。
  • 不,我用的是三星手机
  • Plz selvin 把整个代码发给我,我会检查出来

标签: android geo encoder


【解决方案1】:

试试这个

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");
}    
}

【讨论】:

  • 对于此代码,您必须在 Manifest 中设置权限` `
  • 我已经在我的 Menifest 中设置了这些权限
  • 基本上这一行给出异常
  • 基本上这一行给出异常 .addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
【解决方案2】:

我在我的应用程序中遇到了完全相同的问题,最终编写了自己的 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 );
    }
  }
}

UtilResponseTuple 是我的内部类,我认为,它们的作用很明显

更新:

缺少的方法:

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();
  }
}

和班级:

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 );
  }

}

我也看到,你需要一个reverse geocodig,所以我适当地采用了这个 URL

【讨论】:

  • Util 和 ResponseTuple 这些类在哪里
  • 请把这两门课发给我
猜你喜欢
  • 2013-11-11
  • 1970-01-01
  • 2016-02-02
  • 2015-05-30
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 2014-07-29
  • 1970-01-01
相关资源
最近更新 更多