/** * Gets the reverse geo coding string for a location. * * @param context the context * @param location the location */ private static String getReverseGeoCoding(Context context, Location location) { if (location == null || !ApiAdapterFactory.getApiAdapter().isGeoCoderPresent()) { return null; } Geocoder geocoder = new Geocoder(context); try { List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if (addresses.size() > 0) { Address address = addresses.get(0); int lines = address.getMaxAddressLineIndex(); if (lines > 0) { return address.getAddressLine(0); } String featureName = address.getFeatureName(); if (featureName != null) { return featureName; } String thoroughfare = address.getThoroughfare(); if (thoroughfare != null) { return thoroughfare; } String locality = address.getLocality(); if (locality != null) { return locality; } } } catch (IOException e) { // Can safely ignore } return null; }
/** 위도와 경도 기반으로 주소를 리턴하는 메서드 */ public String getAddress(double lat, double lng) { String address = null; // 위치정보를 활용하기 위한 구글 API 객체 Geocoder geocoder = new Geocoder(context, Locale.getDefault()); // 주소 목록을 담기 위한 HashMap List<Address> list = null; try { list = geocoder.getFromLocation(lat, lng, 1); } catch (Exception e) { e.printStackTrace(); } if (list == null) { Log.e("getAddress", "주소 데이터 얻기 실패"); return null; } if (list.size() > 0) { Address addr = list.get(0); address = // + addr.getPostalCode() + " " addr.getAdminArea() + " " + addr.getLocality() + " " + addr.getThoroughfare(); } return address; }
@Background void task(String r, String num, DialogInterface dialog) { if (currentAddress == null) { return; } String addressCompl = currentAddress.getSubAdminArea() != null ? currentAddress.getSubAdminArea() : currentAddress.getLocality(); List<Address> addresses = GPSUtils.getFromLocationName(getActivity(), r + ", " + num + " - " + (addressCompl)); if (addresses.isEmpty()) { toast(getString(R.string.address_not_found)); dialog.dismiss(); } else { final Address address = addresses.get(0); street = address.getThoroughfare(); if (!TextUtils.isEmpty(num) && StringUtils.isNumeric(num.substring(0, 1))) { number = num; } else { number = ""; } updateUiAdapter(address); } }
@Background void cameraChangeTask() { showHideLoading(true); try { valid = ZupApi.validateCityBoundary(getActivity(), latitude, longitude); if (GPSUtils.getFromLocation(getActivity(), latitude, longitude) == null) { return; } Address addr = GPSUtils.getFromLocation(getActivity(), latitude, longitude).get(0); showHideLoading(false); verifyValid(); if (addr == null) { return; } street = addr.getThoroughfare(); if (!TextUtils.isEmpty(addr.getFeatureName()) && StringUtils.isNumeric(addr.getFeatureName().substring(0, 1))) { number = addr.getFeatureName(); } else { number = ""; } updateUiAdapter(addr); } catch (Exception e) { Log.e("ZUP", e.getMessage(), e); Crashlytics.logException(e); } }
@Background void dialogNumberTask(String num1, DialogInterface dialog) { List<Address> addresses = GPSUtils.getFromLocationName( getActivity(), street + ", " + num1 + " - " + (currentAddress.getSubAdminArea() != null ? currentAddress.getSubAdminArea() : currentAddress.getLocality())); if (addresses.isEmpty()) { toast(getString(R.string.address_not_found)); dialog.dismiss(); } else { final Address address = addresses.get(0); street = address.getThoroughfare(); if (!num1.isEmpty() && StringUtils.isNumeric(num1.substring(0, 1))) { number = num1; } else { number = ""; } updateUiAdapter(address); } }
@Background void timerEnderecoTask(double lat, double lon) { boolean run = true; while (run) { try { Thread.sleep(1000); if (lat != latitude && lon != longitude) { lat = latitude; lon = longitude; if (ignoreUpdate) { ignoreUpdate = false; continue; } showHideLoading(true); List<Address> addresses = GPSUtils.getFromLocation(getContext(), lat, lon); if (addresses.isEmpty()) { showHideLoading(false); return; } Address address = addresses.get(0); if (address.getThoroughfare() == null) { showHideLoading(false); return; } currentAddress = address; valid = ZupApi.validateCityBoundary(getActivity(), lat, lon); verifyValid(); street = address.getThoroughfare(); number = address.getFeatureName(); updateUi(address, address.getFeatureName()); updateUiAdapter(address); showHideLoading(false); } } catch (Exception e) { Log.e("ZUP", e.getMessage(), e); Crashlytics.logException(e); } } }
@UiThread void updateUi(Address addr, String number) { tvNumero.setText(number); autocompleteEndereco.setAdapter(null); autocompleteEndereco.setText(addr.getThoroughfare()); if (getActivity() == null) { return; } autocompleteEndereco.setAdapter( new PlacesAutoCompleteAdapter( getActivity(), R.layout.autocomplete_list_item, SoliciteLocalFragment.class)); autocompleteEndereco.dismissDropDown(); }
private String getStopName(Location location) { Geocoder geocoder = new Geocoder(this); String name = "Unknown"; try { List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if (addresses.size() > 0) { Address address = addresses.get(0); name = address.getThoroughfare(); } } catch (IOException e) { Log.d("Map", "Failed to get name " + e.getMessage()); } return name; }
private void verificaDistancia(Location location) { if (!despertou) { // Update your current location currentLocation = location; if (location != null) { double lat = location.getLatitude(); double lng = location.getLongitude(); // latLongString = "Lat:" + lat + "\nLong:" + lng; Geocoder gc = new Geocoder(this, Locale.getDefault()); try { List<Address> addresses = gc.getFromLocation(lat, lng, 1); StringBuilder sb = new StringBuilder(); if (addresses.size() > 0) { Address address = addresses.get(0); sb.append(address.getThoroughfare()) .append(",") .append(address.getFeatureName()) .append(",") .append(address.getSubLocality()); Log.i("Address", sb.toString()); } map.clear(); setOverlay(); latLng2 = new LatLng(lat, lng); map.addMarker( new MarkerOptions() .position(latLng2) .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus)) .title(this.getString(R.string.posicao_atual)) .snippet(latLng2.toString())); GeoPoint gpOrigem = new GeoPoint((int) (latLng1.latitude * 1E6), (int) (latLng1.longitude * 1E6)); GeoPoint gpDestino = new GeoPoint((int) (latLng2.latitude * 1E6), (int) (latLng2.longitude * 1E6)); double distancia = calculationByDistance(gpOrigem, gpDestino); if (distancia != 0.00f && distancia <= 0.1) { createNoification(sb.toString()); } } catch (IOException e) { } } } }
public static void logAddress(String logTag, Address address) { int maxAddressLineIndex = address.getMaxAddressLineIndex(); for (int i = 0; i <= maxAddressLineIndex; i++) { String addressLine = address.getAddressLine(i); Log.d(logTag, i + ": " + addressLine); } double latitude = 0.0d; double longitude = 0.0d; boolean hasLatitude = address.hasLatitude(); if (hasLatitude) { latitude = address.getLatitude(); } boolean hasLongitude = address.hasLongitude(); if (hasLongitude) { longitude = address.getLongitude(); } String adminArea = address.getAdminArea(); String featureName = address.getFeatureName(); String locality = address.getLocality(); String postalCode = address.getPostalCode(); String premises = address.getPremises(); String subAdminArea = address.getSubAdminArea(); String subLocality = address.getSubLocality(); String subThoroughfare = address.getSubThoroughfare(); String thoroughfare = address.getThoroughfare(); String phone = address.getPhone(); String url = address.getUrl(); // logValue(logTag, "latitude", hasLatitude, latitude); // logValue(logTag, "longitude", hasLongitude, longitude); // logValue(logTag,"adminArea", adminArea); // logValue(logTag,"featureName", featureName); // logValue(logTag,"locality", locality); // logValue(logTag,"postalCode", postalCode); // logValue(logTag,"premises", premises); // logValue(logTag,"subThoroughfare", subThoroughfare); // logValue(logTag,"subAdminArea",subAdminArea); // logValue(logTag,"subLocality", subLocality); // logValue(logTag,"thoroughfare", thoroughfare); // logValue(logTag,"phone", phone); // logValue(logTag,"url", url); }
/** * 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; }
// ------------------------------------------------------------------- // Translate latitude/longitude to street address then populate form // ------------------------------------------------------------------- protected void getCurrentLocation() { // TODO need to fix // progressBar(); // waitForTime(); Location location = m_locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); String message = ""; if (location != null) { message = String.format( "Current Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude()); // displayMessage(message); try { m_longitude = location.getLongitude(); m_latitude = location.getLatitude(); int maxResults = 5; List<Address> addresses = m_geocoder.getFromLocation(m_latitude, m_longitude, maxResults); int size = addresses.size(); if (size != 0) { Address returnedAddress = addresses.get(0); StringBuilder strReturnedAddress = new StringBuilder("Address:\n"); for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) { strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); } /* * Populate address members */ // TODO:DISPLAY ERROR IF NOT IN CORRECT ZIP, STATE, OR CITY /* m_zip = returnedAddress.getPostalCode(); m_state = returnedAddress.getAdminArea(); m_city = returnedAddress.getLocality(); */ // building num m_address = returnedAddress.getSubThoroughfare(); // street m_address = m_address + " " + returnedAddress.getThoroughfare(); message = String.format("Addr: %s", strReturnedAddress.toString()); } else { message = String.format("Error: No Address returned!"); } } catch (IOException e) { e.printStackTrace(); message = String.format("Cannot get Address Please Enter Manually!"); displayMessage(message); } } else { displayMessage("Error getting Location"); } }