/** * Get city from a Location object * * @param context * @param location * @return */ public static String getCity(Context context, Location location) { try { Geocoder geocoder = new Geocoder(context); List<Address> list = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if (list != null && list.size() > 0) { Address address = list.get(0); String result = address.getLocality(); return result; } } catch (Exception e) { // ok } return null; }
/** * Get address from a Location object * * @param context * @param location * @return */ public static String getLocality(Context context, Location location) { if (isConnected(context)) { try { Geocoder geocoder = new Geocoder(context); List<Address> list = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if (list != null && list.size() > 0) { Address address = list.get(0); return address.getThoroughfare() + ", " + address.getLocality(); } } catch (Exception e) { Log.d(TAG, "ignored", e); } } return null; }
public void testReturnsEmpty() { mockery.checking( new Expectations() { { allowing(geoCodeURL).get(); will(returnValue("")); } }); GeocodeInformation info = geo.getGeocodeInformation(); assertTrue(info.isEmpty()); }
@Override protected String doInBackground(Void... params) { Geocoder geocoder = new Geocoder(WriteWeiboActivity.this, Locale.getDefault()); List<Address> addresses = null; try { addresses = geocoder.getFromLocation(geoBean.getLat(), geoBean.getLon(), 1); } catch (IOException e) { cancel(true); } if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); StringBuilder builder = new StringBuilder(); int size = address.getMaxAddressLineIndex(); for (int i = 0; i < size; i++) { builder.append(address.getAddressLine(i)); } return builder.toString(); } return ""; }
public void testSimple() throws InterruptedException { final HttpResponse response = mockery.mock(HttpResponse.class); final StatusLine statusLine = mockery.mock(StatusLine.class); final HttpEntity httpEntity = mockery.mock(HttpEntity.class); mockery.checking( new Expectations() { { allowing(geoCodeURL).get(); will(returnValue("http://foo.com")); try { allowing(httpClient).execute(with(any(HttpUriRequest.class))); } catch (IOException e) { throw new RuntimeException(e); } will(returnValue(response)); allowing(response).getStatusLine(); will(returnValue(statusLine)); allowing(statusLine).getStatusCode(); will(returnValue(HttpStatus.SC_OK)); allowing(response).getEntity(); will(returnValue(httpEntity)); try { allowing(httpEntity).getContent(); } catch (IOException e) { throw new RuntimeException(e); } will(returnValue(new StringInputStream(testS))); allowing(httpEntity).getContentType(); will(returnValue(new BasicHeader("Content-Type", HTTP.DEFAULT_CONTENT_TYPE))); allowing(httpClient).releaseConnection(with(same(response))); } }); GeocodeInformation info = geo.getGeocodeInformation(); // Make sure we have all the correct properties for (GeocodeInformation.Property p : GeocodeInformation.getStrings2Properties().values()) { assertEquals( p.getValue() + " should be in " + info, props.get(p.getValue()), info.getProperty(p)); } }