Here are the examples of the java api class android.location.Address taken from open source projects.
1. ShadowGeocoder#getFromLocation()
Project: robolectric
File: ShadowGeocoder.java
File: ShadowGeocoder.java
@Implementation public List<Address> getFromLocation(double latitude, double longitude, int maxResults) throws IOException { wasCalled = true; this.lastLatitude = latitude; this.lastLongitude = longitude; if (shouldSimulateGeocodeException) { throw new IOException("Simulated geocode exception"); } Address address = new Address(Locale.getDefault()); address.setAddressLine(0, addressLine1); address.setLocality(city); address.setAdminArea(state); address.setPostalCode(zip); address.setCountryCode(countryCode); return oneElementList(address); }
2. MapQuestGeocoder#mapquestToAddress()
Project: cgeo
File: MapQuestGeocoder.java
File: MapQuestGeocoder.java
private static Address mapquestToAddress(final JsonNode mapquestAddress) { final Address address = new Address(Locale.getDefault()); for (int i = 1; i <= 6; i++) { final String adminAreaName = "adminArea" + i; setComponent(address, mapquestAddress, adminAreaName, mapquestAddress.path(adminAreaName + "Type").asText()); } setComponent(address, mapquestAddress, "postalCode", "PostalCode"); int index = 0; for (final String addressComponent : new String[] { mapquestAddress.path("street").asText(), address.getSubLocality(), address.getLocality(), address.getPostalCode(), address.getSubAdminArea(), address.getAdminArea(), address.getCountryCode() }) { if (StringUtils.isNotBlank(addressComponent)) { address.setAddressLine(index++, addressComponent); } } final JsonNode latLng = mapquestAddress.get("latLng"); address.setLatitude(latLng.get("lat").asDouble()); address.setLongitude(latLng.get("lng").asDouble()); return address; }
3. GeocoderUtils#getAddressFromLocationName()
Project: WordPress-Android
File: GeocoderUtils.java
File: GeocoderUtils.java
public static Address getAddressFromLocationName(Context context, String locationName) { int maxResults = 1; Address address = null; List<Address> addresses = null; Geocoder gcd = getGeocoder(context); if (gcd == null) { return null; } try { addresses = gcd.getFromLocationName(locationName, maxResults); } catch (IOException e) { AppLog.e(AppLog.T.UTILS, "Failed to get coordinates from location", e); } // addresses may be null or empty if network isn't connected if (addresses != null && addresses.size() > 0) { address = addresses.get(0); } return address; }
4. GeocoderNominatim#buildAndroidAddress()
Project: osmbonuspack
File: GeocoderNominatim.java
File: GeocoderNominatim.java
/** * Build an Android Address object from the Nominatim address in JSON format. * Current implementation is mainly targeting french addresses, * and will be quite basic on other countries. * @return Android Address, or null if input is not valid. */ protected Address buildAndroidAddress(JsonObject jResult) throws JsonSyntaxException { Address gAddress = new Address(mLocale); if (!jResult.has("lat") || !jResult.has("lon") || !jResult.has("address")) return null; gAddress.setLatitude(jResult.get("lat").getAsDouble()); gAddress.setLongitude(jResult.get("lon").getAsDouble()); JsonObject jAddress = jResult.get("address").getAsJsonObject(); int addressIndex = 0; if (jAddress.has("road")) { gAddress.setAddressLine(addressIndex++, jAddress.get("road").getAsString()); gAddress.setThoroughfare(jAddress.get("road").getAsString()); } if (jAddress.has("suburb")) { //gAddress.setAddressLine(addressIndex++, jAddress.getString("suburb")); //not kept => often introduce "noise" in the address. gAddress.setSubLocality(jAddress.get("suburb").getAsString()); } if (jAddress.has("postcode")) { gAddress.setAddressLine(addressIndex++, jAddress.get("postcode").getAsString()); gAddress.setPostalCode(jAddress.get("postcode").getAsString()); } if (jAddress.has("city")) { gAddress.setAddressLine(addressIndex++, jAddress.get("city").getAsString()); gAddress.setLocality(jAddress.get("city").getAsString()); } else if (jAddress.has("town")) { gAddress.setAddressLine(addressIndex++, jAddress.get("town").getAsString()); gAddress.setLocality(jAddress.get("town").getAsString()); } else if (jAddress.has("village")) { gAddress.setAddressLine(addressIndex++, jAddress.get("village").getAsString()); gAddress.setLocality(jAddress.get("village").getAsString()); } if (//France: departement jAddress.has("county")) { gAddress.setSubAdminArea(jAddress.get("county").getAsString()); } if (//France: region jAddress.has("state")) { gAddress.setAdminArea(jAddress.get("state").getAsString()); } if (jAddress.has("country")) { gAddress.setAddressLine(addressIndex++, jAddress.get("country").getAsString()); gAddress.setCountryName(jAddress.get("country").getAsString()); } if (jAddress.has("country_code")) gAddress.setCountryCode(jAddress.get("country_code").getAsString()); /* Other possible OSM tags in Nominatim results not handled yet: * subway, golf_course, bus_stop, parking,... * house, house_number, building * city_district (13e Arrondissement) * road => or highway, ... * sub-city (like suburb) => locality, isolated_dwelling, hamlet ... * state_district */ //Add non-standard (but very useful) information in Extras bundle: Bundle extras = new Bundle(); if (jResult.has("polygonpoints")) { JsonArray jPolygonPoints = jResult.get("polygonpoints").getAsJsonArray(); ArrayList<GeoPoint> polygonPoints = new ArrayList<GeoPoint>(jPolygonPoints.size()); for (int i = 0; i < jPolygonPoints.size(); i++) { JsonArray jCoords = jPolygonPoints.get(i).getAsJsonArray(); double lon = jCoords.get(0).getAsDouble(); double lat = jCoords.get(1).getAsDouble(); GeoPoint p = new GeoPoint(lat, lon); polygonPoints.add(p); } extras.putParcelableArrayList("polygonpoints", polygonPoints); } if (jResult.has("boundingbox")) { JsonArray jBoundingBox = jResult.get("boundingbox").getAsJsonArray(); BoundingBoxE6 bb = new BoundingBoxE6(jBoundingBox.get(1).getAsDouble(), jBoundingBox.get(2).getAsDouble(), jBoundingBox.get(0).getAsDouble(), jBoundingBox.get(3).getAsDouble()); extras.putParcelable("boundingbox", bb); } if (jResult.has("osm_id")) { long osm_id = jResult.get("osm_id").getAsLong(); extras.putLong("osm_id", osm_id); } if (jResult.has("osm_type")) { String osm_type = jResult.get("osm_type").getAsString(); extras.putString("osm_type", osm_type); } if (jResult.has("display_name")) { String display_name = jResult.get("display_name").getAsString(); extras.putString("display_name", display_name); } gAddress.setExtras(extras); return gAddress; }
5. ShadowGeocoder#getFromLocationName()
Project: robolectric
File: ShadowGeocoder.java
File: ShadowGeocoder.java
@Implementation public List<Address> getFromLocationName(String locationName, int maxResults) throws IOException { this.lastLocationName = locationName; if (shouldSimulateGeocodeException) { throw new IOException("Simulated geocode exception"); } Address address = new Address(Locale.getDefault()); address.setLatitude(simulatedLatitude); address.setLongitude(simulatedLongitude); return oneElementList(address); }
6. GeocoderGisgraphy#buildAndroidAddress()
Project: osmbonuspack
File: GeocoderGisgraphy.java
File: GeocoderGisgraphy.java
/** * Build an Android Address object from the Gisgraphy address in JSON format. */ protected Address buildAndroidAddress(JSONObject jResult) throws JSONException { Address gAddress = new Address(mLocale); gAddress.setLatitude(jResult.getDouble("lat")); gAddress.setLongitude(jResult.getDouble("lng")); int addressIndex = 0; if (jResult.has("streetName")) { gAddress.setAddressLine(addressIndex++, jResult.getString("streetName")); gAddress.setThoroughfare(jResult.getString("streetName")); } /* if (jResult.has("suburb")){ //gAddress.setAddressLine(addressIndex++, jResult.getString("suburb")); //not kept => often introduce "noise" in the address. gAddress.setSubLocality(jResult.getString("suburb")); } */ if (jResult.has("zipCode")) { gAddress.setAddressLine(addressIndex++, jResult.getString("zipCode")); gAddress.setPostalCode(jResult.getString("zipCode")); } if (jResult.has("city")) { gAddress.setAddressLine(addressIndex++, jResult.getString("city")); gAddress.setLocality(jResult.getString("city")); } if (//France: region jResult.has("state")) { gAddress.setAdminArea(jResult.getString("state")); } if (jResult.has("country")) { gAddress.setAddressLine(addressIndex++, jResult.getString("country")); gAddress.setCountryName(jResult.getString("country")); } if (jResult.has("countrycode")) gAddress.setCountryCode(jResult.getString("countrycode")); return gAddress; }
7. GeocoderUtils#getAddressFromCoords()
Project: WordPress-Android
File: GeocoderUtils.java
File: GeocoderUtils.java
public static Address getAddressFromCoords(Context context, double latitude, double longitude) { Address address = null; List<Address> addresses = null; Geocoder gcd = getGeocoder(context); if (gcd == null) { return null; } try { addresses = gcd.getFromLocation(latitude, longitude, 1); } catch (IOException e) { AppLog.e(AppLog.T.UTILS, "Unable to parse response from server. Is Geocoder service hitting the server too frequently?", e); } // addresses may be null or empty if network isn't connected if (addresses != null && addresses.size() > 0) { address = addresses.get(0); } return address; }
8. DBResultActivity#setupQuery()
Project: wigle-wifi-wardriving
File: DBResultActivity.java
File: DBResultActivity.java
@SuppressLint("HandlerLeak") private void setupQuery(final QueryArgs queryArgs) { final Address address = queryArgs.getAddress(); // what runs on the gui thread final Handler handler = new Handler() { @Override public void handleMessage(final Message msg) { final TextView tv = (TextView) findViewById(R.id.dbstatus); if (msg.what == MSG_QUERY_DONE) { tv.setText(getString(R.string.status_success)); listAdapter.clear(); boolean first = true; for (final Network network : resultList) { listAdapter.add(network); if (address == null && first) { final LatLng center = MappingFragment.getCenter(DBResultActivity.this, network.getLatLng(), null); MainActivity.info("set center: " + center + " network: " + network.getSsid() + " point: " + network.getLatLng()); mapView.getMapAsync(new OnMapReadyCallback() { @Override public void onMapReady(final GoogleMap googleMap) { final CameraPosition cameraPosition = new CameraPosition.Builder().target(center).zoom(DEFAULT_ZOOM).build(); googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); } }); first = false; } if (network.getLatLng() != null && mapRender != null) { mapRender.addItem(network); } } resultList.clear(); } } }; String sql = "SELECT bssid,lastlat,lastlon FROM " + DatabaseHelper.NETWORK_TABLE + " WHERE 1=1 "; final String ssid = queryArgs.getSSID(); final String bssid = queryArgs.getBSSID(); boolean limit = false; if (ssid != null && !"".equals(ssid)) { sql += " AND ssid like " + DatabaseUtils.sqlEscapeString(ssid); limit = true; } if (bssid != null && !"".equals(bssid)) { sql += " AND bssid like " + DatabaseUtils.sqlEscapeString(bssid); limit = true; } if (address != null) { final double diff = 0.1d; final double lat = address.getLatitude(); final double lon = address.getLongitude(); sql += " AND lastlat > '" + (lat - diff) + "' AND lastlat < '" + (lat + diff) + "'"; sql += " AND lastlon > '" + (lon - diff) + "' AND lastlon < '" + (lon + diff) + "'"; } if (limit) { sql += " LIMIT " + LIMIT; } final TreeMap<Float, String> top = new TreeMap<>(); final float[] results = new float[1]; final long[] count = new long[1]; final QueryThread.Request request = new QueryThread.Request(sql, new QueryThread.ResultHandler() { @Override public boolean handleRow(final Cursor cursor) { final String bssid = cursor.getString(0); final float lat = cursor.getFloat(1); final float lon = cursor.getFloat(2); count[0]++; if (address == null) { top.put((float) count[0], bssid); } else { Location.distanceBetween(lat, lon, address.getLatitude(), address.getLongitude(), results); final float meters = results[0]; if (top.size() <= LIMIT) { putWithBackoff(top, bssid, meters); } else { Float last = top.lastKey(); if (meters < last) { top.remove(last); putWithBackoff(top, bssid, meters); } } } return true; } @Override public void complete() { for (final String bssid : top.values()) { final Network network = ListFragment.lameStatic.dbHelper.getNetwork(bssid); resultList.add(network); final LatLng point = network.getLatLng(); if (point != null) { obsMap.put(point, 0); } } handler.sendEmptyMessage(MSG_QUERY_DONE); if (mapView != null) { // force a redraw mapView.postInvalidate(); } } }); // queue it up ListFragment.lameStatic.dbHelper.addToQueue(request); }
9. LocationActivity#onCreate()
Project: socialize-sdk-android
File: LocationActivity.java
File: LocationActivity.java
@Override protected void onCreate() { setContentView(R.layout.location); Location lastKnownLocation = LocationUtils.getLastKnownLocation(this); TextView text = (TextView) findViewById(R.id.txtLocation); Button btnLocation = (Button) findViewById(R.id.btnLocation); Address geoCoded = null; if (lastKnownLocation != null) { try { geoCoded = geoCode(lastKnownLocation); } catch (IOException e) { e.printStackTrace(); } } if (geoCoded != null) { text.setText(geoCoded.getLocality()); } else { text.setText("Unknown"); } btnLocation.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LocationUtils.updateLocation(LocationActivity.this); Toast.makeText(LocationActivity.this, "Update requested", Toast.LENGTH_SHORT).show(); } }); }
10. GeocoderTest#testGeocoder()
Project: cgeo
File: GeocoderTest.java
File: GeocoderTest.java
public static void testGeocoder(final Observable<Address> addressObservable, final String geocoder, final boolean withAddress) { final Address address = addressObservable.toBlocking().first(); assertThat(address.getLatitude()).as(describe("latitude", geocoder)).isCloseTo(TEST_LATITUDE, TEST_OFFSET); assertThat(address.getLongitude()).as(describe("longitude", geocoder)).isCloseTo(TEST_LONGITUDE, TEST_OFFSET); if (withAddress) { assertThat(StringUtils.lowerCase(address.getAddressLine(0))).as(describe("street address", geocoder)).contains("barrault"); assertThat(address.getLocality()).as(describe("locality", geocoder)).isEqualTo("Paris"); assertThat(address.getCountryCode()).as(describe("country code", geocoder)).isEqualTo("FR"); // don't assert on country name, as this can be localized, e.g. with the mapquest geocoder } }
11. AddressListAdapter#getView()
Project: cgeo
File: AddressListAdapter.java
File: AddressListAdapter.java
@Override public View getView(final int position, final View convertView, final ViewGroup parent) { final Address address = getItem(position); View view = convertView; // holder pattern implementation final ViewHolder holder; if (view == null) { view = inflater.inflate(R.layout.addresslist_item, parent, false); holder = new ViewHolder(view); } else { holder = (ViewHolder) view.getTag(); } view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(final View v) { final Activity activity = (Activity) v.getContext(); CacheListActivity.startActivityAddress(activity, new Geopoint(address.getLatitude(), address.getLongitude()), StringUtils.defaultString(address.getAddressLine(0))); activity.finish(); } }); holder.label.setText(getAddressText(address)); holder.distance.setText(getDistanceText(address)); return view; }