@Override public void onMapLongClick(LatLng point) { List<Address> namelist = null; Address a; if (source.equals(null)) { googleMap.addMarker(new MarkerOptions().position(point).title("Source")); Geocoder gcd = new Geocoder(this); try { namelist = gcd.getFromLocation(point.latitude, point.longitude, 2); } catch (IOException e) { e.printStackTrace(); } a = namelist.get(0); editS.setText(a.getLocality()); } else { googleMap.addMarker(new MarkerOptions().position(point).title("Destination")); Geocoder gcd = new Geocoder(this); try { namelist = gcd.getFromLocation(point.latitude, point.longitude, 2); } catch (IOException e) { e.printStackTrace(); } a = namelist.get(0); editD.setText(a.getLocality()); } }
@Override protected String doInBackground(Location... params) { Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault()); // get current location from input parameter list Location location = params[0]; List<Address> addresses = null; try { /* * Call getFromLocation() method with lat, lng of current * location and return at most 1 result */ addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); } catch (IOException e) { e.printStackTrace(); return null; } if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); String addressString = address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : ""; addressString = addressString.concat(", " + address.getLocality() + ", " + address.getCountryName()); addressStr = address.getLocality() + ", " + address.getCountryName(); return addressString; } else { return null; } }
@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 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); } }
private void updateWithNewLocation(Location location) { if (location != null) { double lat = location.getLatitude(); double lng = location.getLongitude(); Geocoder gc = new Geocoder(MainActivity.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); String addressName = address.getSubLocality(); String firstCityName = address.getAdminArea(); infoToShow.locationName = StringProcessing.FilterStr(addressName); mAddressCityNameHan = infoToShow.locationName; locationCityName = infoToShow.locationName; // mAddressCityName = // CN2Pinyin.converterToSpell(infoToShow.locationName); mAdminArea = StringProcessing.FilterStr(firstCityName); sb.append(address.getLocality()) .append("/") .append(address.getSubLocality()) .append("\n"); } } catch (Exception e) { Log.d(e.toString(), "error!"); } } }
@Override protected Void doInBackground(Location... params) { Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); Location loc = params[0]; List<Address> addresses = null; try { addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); } catch (IOException e) { e.printStackTrace(); // Update address field with the exception. Message.obtain(handler, UPDATE_ADDRESS, e.toString()).sendToTarget(); } if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); // Format the first line of address (if available), city, and country name String addressText = String.format( "%s, %s, %s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", address.getLocality(), address.getCountryName()); // Update address field on UI Message.obtain(handler, UPDATE_ADDRESS, addressText).sendToTarget(); } return null; }
private void getLocality(Location location) { if (location != null) { longitude = location.getLongitude(); latitude = location.getLatitude(); try { address = (Address) geocoder.getFromLocation(latitude, longitude, 1).toArray()[0]; String locality = address.getSubAdminArea(); if (locality == null) { locality = address.getLocality(); } if (locality == null) { locality = address.getSubAdminArea(); } if (locality == null) { locality = address.getAddressLine(0); } Toast t = Toast.makeText(getApplicationContext(), locality, Toast.LENGTH_LONG); t.show(); } catch (Exception e) { System.out.println(e); // } // longitude = location.getLongitude(); // latitude = location.getLatitude(); // Toast t = Toast.makeText(getApplicationContext(), // String.format("long: " + longitude + "\nlat: " + latitude), // Toast.LENGTH_LONG); // t.show(); } } else { Toast t = Toast.makeText(getApplicationContext(), "NO LAST LOCATION", Toast.LENGTH_SHORT); t.show(); } }
/** 위도와 경도 기반으로 주소를 리턴하는 메서드 */ 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; }
public String GetClosestCityName() { String result = "nada"; Geocoder geocoder = new Geocoder(this); LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); if (lm != null) { Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); List<Address> list = new ArrayList<Address>(); if (location != null) { try { list = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 4); } catch (IOException e) { e.printStackTrace(); } if (list != null) { if (list.size() > 0) { String locality; for (Address address : list) { locality = address.getLocality(); if (locality != null) result = locality; } } } } } return result; }
private AlertDialog makeLocationChoiceDialogBox() { final String[] addressStrings = new String[addresses.size()]; Address address = null; openSoftKeyboard(); for (int index = 0; index < addresses.size(); ++index) { address = addresses.get(index); addressStrings[index] = (address.getAddressLine(0) + "\n" + address.getLocality() + "\n" + address.getCountryName()); } AlertDialog LocationChoice = new AlertDialog.Builder(this) .setTitle("Select Location") .setItems( addressStrings, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichChoice) { setLocation(addressStrings[whichChoice].toString()); Address chosenAddress = addresses.get(whichChoice); setLocationLatitude(chosenAddress.getLatitude()); setLocationLongitude(chosenAddress.getLongitude()); setProximity(1); dialog.dismiss(); } }) .create(); return LocationChoice; }
/** * 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; }
private void getAddressFromLocation(final LatLng latlng, final MyFontTextView et) { /* * et.setText("Waiting for Address"); et.setTextColor(Color.GRAY); */ /* * new Thread(new Runnable() { * * @Override public void run() { // TODO Auto-generated method stub */ Geocoder gCoder = new Geocoder(getActivity()); try { final List<Address> list = gCoder.getFromLocation(latlng.latitude, latlng.longitude, 1); if (list != null && list.size() > 0) { Address address = list.get(0); StringBuilder sb = new StringBuilder(); if (address.getAddressLine(0) != null) { sb.append(address.getAddressLine(0)).append(", "); } sb.append(address.getLocality()).append(", "); // sb.append(address.getPostalCode()).append(","); sb.append(address.getCountryName()); strAddress = sb.toString(); strAddress = strAddress.replace(",null", ""); strAddress = strAddress.replace("null", ""); strAddress = strAddress.replace("Unnamed", ""); if (!TextUtils.isEmpty(strAddress)) { et.setText(strAddress); } } /* * getActivity().runOnUiThread(new Runnable() { * * @Override public void run() { // TODO Auto-generated method stub * if (!TextUtils.isEmpty(strAddress)) { * * et.setText(strAddress); * * * } else { et.setText(""); * * } * * } }); */ } catch (IOException exc) { exc.printStackTrace(); } // } // }).start(); }
public GeoPoint(@NonNull Address address) { if (address == null) { return; } this.name = address.getLocality(); this.latitude = address.getLatitude(); this.longitude = address.getLongitude(); }
/** * Get a Geocoder instance, get the latitude and longitude look up the address, and return it * * @params params One or more Latlng objects * @return A string containing the address of the current location, or an empty string if no * address can be found, or an error message */ @Override protected MarkerPos doInBackground(MarkerPos... params) { Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); // Get the current location from the input parameter list MarkerPos markpos = new MarkerPos(params[0]); LatLng loc = markpos.getPosition(); // Create a list to contain the result address List<Address> addresses = null; try { /* * Return 1 address. */ addresses = geocoder.getFromLocation(loc.latitude, loc.longitude, 1); } catch (IOException e1) { Log.e("LocationSampleActivity", "IO Exception in getFromLocation()"); e1.printStackTrace(); markpos.setAdresse("Impossible d'avoir l'adresse. Vérifier connexion réseau"); return markpos; } catch (IllegalArgumentException e2) { // Error message to post in the log String errorString = "Illegal arguments " + Double.toString(loc.latitude) + " , " + Double.toString(loc.longitude) + " passed to address service"; Log.e("LocationSampleActivity", errorString); e2.printStackTrace(); markpos.setAdresse(errorString); return markpos; } // If the reverse geocode returned an address if (addresses != null && addresses.size() > 0) { // Get the first address Address address = addresses.get(0); /* * Format the first line of address (if available), * city, and country name. */ String addressText = String.format( "%s, %s, %s", // If there's a street address, add it address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", // Locality is usually a city address.getLocality(), // The country of the address address.getCountryName()); // Return the text markpos.setAdresse(addressText); return markpos; } else { markpos.setAdresse("No address found"); return markpos; } }
public String getLocality(Context context) { List<Address> addresses = getGeocoderAddress(context); if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); String locality = address.getLocality(); return locality; } else { return null; } }
public String getCity(double lat, double lon) { Geocoder geocode = new Geocoder(mContext, Locale.getDefault()); try { List<Address> addresses = geocode.getFromLocation(lat, lon, 1); if (addresses != null) { Address fetchedAddress = addresses.get(0); return fetchedAddress.getLocality(); } } catch (IOException e) { return ""; } return ""; }
/** * Format the address * * @param address the address that need to be formated * @return formated address */ private static String formatAddress(Address address) { StringBuilder stringBuilder = new StringBuilder(); String location = ""; location = address.getLocality(); stringBuilder.append( location != null ? (location + ", ") : (address.getSubAdminArea() != null) ? (address.getSubAdminArea() + ", ") : ""); location = address.getAdminArea(); stringBuilder.append(location != null ? (location + ", ") : ""); stringBuilder.append(location != null ? address.getCountryName() : address.getCountryCode()); return stringBuilder.toString(); }
/** * Returns a simple one-line address based on city and country * * @param address * @return */ public String getSimpleLocation(Address address) { StringBuilder builder = new StringBuilder(); String locality = address.getLocality(); String countryName = address.getCountryName(); if (!StringUtils.isEmpty(locality)) { builder.append(locality); } else if (!StringUtils.isEmpty(countryName)) { builder.append(countryName); } return builder.toString(); }
@Override protected String doInBackground(Location... params) { Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); // Get the current location from the input parameter list Location loc = params[0]; // Create a list to contain the result address List<Address> addresses = null; try { /* * Return 1 address. */ addresses = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1); } catch (IOException e1) { Log.e("LocationSampleActivity", "IO Exception in getFromLocation()"); e1.printStackTrace(); return ("IO Exception trying to get address"); } catch (IllegalArgumentException e2) { // Error message to post in the log String errorString = "Illegal arguments " + Double.toString(loc.getLatitude()) + " , " + Double.toString(loc.getLongitude()) + " passed to address service"; Log.e("LocationSampleActivity", errorString); e2.printStackTrace(); return errorString; } // If the reverse geocode returned an address if (addresses != null && addresses.size() > 0) { // Get the first address Address address = addresses.get(0); /* * Format the first line of address (if available), * city, and country name. */ String addressText = String.format( "%s, %s", // If there's a street address, add it address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", // Locality is usually a city address.getLocality()); // Return the text return addressText; } else { return "No address found"; } }
@Override protected String doInBackground(Location... locations) { Geocoder geocoder = new Geocoder(_context, Locale.getDefault()); Location location = locations[0]; List<Address> addresses = null; try { addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); } catch (IOException ex1) { Log.e(LocationUtils.APPTAG, getString(R.string.IO_Exception_getFromLocation)); ex1.printStackTrace(); return (getString(R.string.IO_Exception_getFromLocation)); } catch (IllegalArgumentException ex2) { String errorString = getString( R.string.illegal_argument_exception, location.getLatitude(), location.getLongitude()); Log.e(LocationUtils.APPTAG, errorString); ex2.printStackTrace(); return errorString; } if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); String addressText = getString( R.string.address_output_string, // if there is a street address address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", // locality is the city address.getLocality(), address.getCountryName()); return addressText; } else { return getString(R.string.no_address_found); } }
private void updateWithNewLocation(Location location) { TextView myLocationText; myLocationText = (TextView) findViewById(R.id.myLocationText); String latLongString = "No location found"; String addressString = "No address found"; if (location != null) { // Update the position overlay. positionOverlay.setLocation(location); Double geoLat = location.getLatitude() * 1E6; Double geoLng = location.getLongitude() * 1E6; GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue()); mapController.animateTo(point); double lat = location.getLatitude(); double lng = location.getLongitude(); latLongString = "Lat:" + lat + "\nLong:" + lng; double latitude = location.getLatitude(); double longitude = location.getLongitude(); Geocoder gc = new Geocoder(this, Locale.getDefault()); if (!Geocoder.isPresent()) addressString = "No geocoder available"; else { try { List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); StringBuilder sb = new StringBuilder(); if (addresses.size() > 0) { Address address = addresses.get(0); for (int i = 0; i < address.getMaxAddressLineIndex(); i++) sb.append(address.getAddressLine(i)).append("\n"); sb.append(address.getLocality()).append("\n"); sb.append(address.getPostalCode()).append("\n"); sb.append(address.getCountryName()); } addressString = sb.toString(); } catch (IOException e) { Log.d("WHEREAMI", "IO Exception", e); } } } myLocationText.setText("Your Current Position is:\n" + latLongString + "\n\n" + addressString); }
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); }
/** * Update the note using the details provided. The note to be updated is specified using the * rowId, and it is altered to use the title and body values passed in * * @param rowId id of note to update * @param title value to set note title to * @param body value to set note body to * @return true if the note was successfully updated, false otherwise */ public boolean updateTrack(long rowId, MusicTrack track, Address a) { ContentValues initialValues = new ContentValues(); Date d = new Date(); initialValues.put(KEY_DATETIME, d.getTime()); initialValues.put(KEY_TRACK, track.mTrackName); initialValues.put(KEY_ALBUM, track.mAlbumName); initialValues.put(KEY_ARTIST, track.mArtistName); initialValues.put(KEY_CITY, a.getLocality()); initialValues.put(KEY_STATE, a.getAdminArea()); initialValues.put(KEY_COUNTRY, a.getCountryName()); initialValues.put(KEY_FEATURE, a.getFeatureName()); initialValues.put(KEY_LON, a.getLongitude()); initialValues.put(KEY_LAT, a.getLatitude()); return mDb.update(DATABASE_TABLE, initialValues, KEY_ROWID + "=" + rowId, null) > 0; }
/** * 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; }
private static String formatAddress(final Address address) { final ArrayList<String> addressParts = new ArrayList<String>(); final String countryName = address.getCountryName(); if (countryName != null) { addressParts.add(countryName); } final String locality = address.getLocality(); if (locality != null) { addressParts.add(locality); } else { final String adminArea = address.getAdminArea(); if (adminArea != null) { addressParts.add(adminArea); } } return StringUtils.join(addressParts, ", "); }
@Override protected boolean onTap(int index) { // nothing so far. mMapController.animateTo(mGeoPoint); // show the addr in the toast box. Address address = mAddr.get(0); Toast.makeText( MapViewMain.this, address.getAddressLine(0) + ", " + address.getLocality() + ", " + address.getCountryName(), Toast.LENGTH_LONG) .show(); return true; }
/** * 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; }
/** * Create a new note using the title and body provided. If the note is successfully created return * the new rowId for that note, otherwise return a -1 to indicate failure. * * @param title the title of the note * @param body the body of the note * @return rowId or -1 if failed */ public long insertTrack(MusicTrack track, Address a) { Date d = new Date(); ContentValues initialValues = new ContentValues(); initialValues.put(KEY_DATETIME, d.getTime()); initialValues.put(KEY_TRACK, track.mTrackName); initialValues.put(KEY_ALBUM, track.mAlbumName); initialValues.put(KEY_ARTIST, track.mArtistName); if (a != null) { initialValues.put(KEY_CITY, a.getLocality()); initialValues.put(KEY_STATE, a.getAdminArea()); initialValues.put(KEY_COUNTRY, a.getCountryName()); initialValues.put(KEY_FEATURE, a.getFeatureName()); initialValues.put(KEY_LON, a.getLongitude()); initialValues.put(KEY_LAT, a.getLatitude()); } return mDb.insert(DATABASE_TABLE, null, initialValues); }
public String getLocationName() { if (!isConnected()) { return "Network is disabled"; } else { List<Address> addresses = null; Geocoder geocoder = new Geocoder(context.getApplicationContext(), Locale.getDefault()); try { addresses = geocoder.getFromLocation(getLatitude(), getLongitude(), 1); } catch (IOException e) { e.printStackTrace(); } Address address = addresses.get(0); return address.getCountryName() + ", " + address.getLocality() + ", " + address.getAdminArea() + ", " + address.getAddressLine(0); } }
@Override protected String doInBackground(Type... params) { if (((FOGmain) ((Activity) callingAcitivity).getApplicationContext()).isNetworkConnected() == false) { return "no_connection"; } else { Geocoder geocoder = new Geocoder(((Activity) callingAcitivity).getBaseContext(), Locale.getDefault()); addresses = new ArrayList<Address>(); type = params[0]; if (Geocoder.isPresent()) { try { if (type == Type.ADDRESSTOLOCATION) { addresses = geocoder.getFromLocationName(addressText, 5); return "AddressToLocation"; } else if (type == Type.LOCATIONTOADDRESS) { addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); addressText = String.format( "%s, %s, %s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "", address.getLocality(), address.getCountryName()); return addressText; } } } catch (IOException e) { e.printStackTrace(); } } return null; } }