List of usage examples for android.location Address getAddressLine
public String getAddressLine(int index)
From source file:org.redbus.ui.stopmap.StopMapActivity.java
public void onAsyncGeocodeResponseSuccess(int requestId, List<Address> addresses_) { Log.i(TAG, "Async Geocode success!"); if (requestId != expectedRequestId) return;/*w w w .j ava 2 s . c om*/ if (busyDialog != null) busyDialog.dismiss(); if (addresses_.size() == 1) { Address address = addresses_.get(0); LatLng pos = new LatLng(address.getLatitude(), address.getLongitude()); zoomTo(pos); return; } final List<Address> addresses = addresses_; List<String> addressNames = new ArrayList<String>(); for (Address a : addresses) { StringBuilder strb = new StringBuilder(); for (int i = 0; i < a.getMaxAddressLineIndex(); i++) { if (i > 0) strb.append(", "); strb.append(a.getAddressLine(i)); } addressNames.add(strb.toString()); } String[] addressArray = addressNames.toArray(new String[addressNames.size()]); DialogInterface.OnClickListener onClickSetPosition = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { if (which < 0) return; Address address = addresses.get(which); LatLng pos = new LatLng(address.getLatitude(), address.getLongitude()); zoomTo(pos); dialog.dismiss(); } }; new AlertDialog.Builder(this).setSingleChoiceItems(addressArray, -1, onClickSetPosition).show(); }
From source file:no.ntnu.idi.socialhitchhiking.map.GeoHelper.java
/** * Retrieves a {@link List} of addresses that match the given {@link GeoPoint}. * The first element in the list has the best match (but is not guaranteed to be correct). <br><br> * /*from w w w. j av a 2s. c o m*/ * This method tries to use the {@link Geocoder} to transform a (latitude, longitude) * coordinate into addresses, and if this fails (witch it most likely will under emulation), it * tries to use a method from the {@link GeoHelper}-class. * * @param location The location that is transformed into a list of addresses * @param maxResults The maximum number of addresses to retrieve (should be small). * @param maxAddressLines The maximum number of lines in the addresses. This should be high if you want a complete address! If it is smaller than the total number of lines in the address, it cuts off the last part...) * @return Returns the {@link List} of addresses (as {@link String}s). */ public static List<String> getAddressesAtPoint(final GeoPoint location, final int maxResults, int maxAddressLines) { List<String> addressList = new ArrayList<String>(); List<Address> possibleAddresses = new ArrayList<Address>(); Address address = new Address(Locale.getDefault()); String addressString = "Could not find the address..."; ExecutorService executor = Executors.newSingleThreadExecutor(); Callable<List<Address>> callable = new Callable<List<Address>>() { @Override public List<Address> call() throws IOException { return fancyGeocoder.getFromLocation(location.getLatitudeE6() / 1E6, location.getLongitudeE6() / 1E6, maxResults); } }; Future<List<Address>> future = executor.submit(callable); try { possibleAddresses = future.get(); } catch (InterruptedException e1) { possibleAddresses = GeoHelper.getAddressesFromLocation(location.getLatitudeE6() / 1E6, location.getLongitudeE6() / 1E6, maxResults); } catch (ExecutionException e1) { possibleAddresses = GeoHelper.getAddressesFromLocation(location.getLatitudeE6() / 1E6, location.getLongitudeE6() / 1E6, maxResults); } executor.shutdown(); if (possibleAddresses.size() > 0) { for (int i = 0; i < possibleAddresses.size(); i++) { addressString = ""; address = possibleAddresses.get(i); for (int j = 0; j <= address.getMaxAddressLineIndex() && j <= maxAddressLines; j++) { addressString += address.getAddressLine(j); addressString += "\n"; } addressList.add(addressString.trim()); } } return addressList; }
From source file:net.sylvek.sharemyposition.ShareMyPosition.java
public String getAddress(double latitude, double longitude) { List<Address> address = null; try {/*from w w w . j ava 2 s .c o m*/ address = gc.getFromLocation(latitude, longitude, 1); } catch (IOException e) { Log.e(LOG, "unable to get address", e); return ""; } if (address == null || address.size() == 0) { Log.w(LOG, "unable to parse address"); return ""; } Address a = address.get(0); StringBuilder b = new StringBuilder(); for (int i = 0; i < a.getMaxAddressLineIndex(); i++) { b.append(a.getAddressLine(i)); if (i < (a.getMaxAddressLineIndex() - 1)) { b.append(" "); } } return b.toString(); }
From source file:tw.com.geminihsu.app01.fragment.Fragment_Client_Service_test.java
private void getCurrentAddress(double longitude, double latitude) { Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault()); try {/*from w w w . ja v a2 s .c o m*/ List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1); if (addressList != null && addressList.size() > 0) { Address address = addressList.get(0); StringBuilder sb = new StringBuilder(); 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()); result.setLatitude(latitude); result.setLongitude(longitude); result.setCountryName(address.getCountryName()); result.setLocality(address.getLocality()); result.setZipCode(address.getPostalCode()); result.setLocation(sb.toString()); if (address.getCountryCode().equals("TW")) { result.setAddress(address.getAddressLine(0).substring(3, address.getAddressLine(0).length())); result.setLocation(address.getAddressLine(0).substring(3, address.getAddressLine(0).length())); } else { result.setAddress(sb.toString()); result.setLocation(sb.toString()); } curAddress = result.getAddress(); } } catch (IOException e) { Log.e("", "Unable connect to Geocoder", e); } //test = true; }
From source file:tw.com.geminihsu.app01.fragment.Fragment_Client_Service.java
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getCurrentGPSLocationBroadcastReceiver = new BroadcastReceiver() { @Override//from w w w . j ava 2 s. c om public void onReceive(Context context, Intent intent) { // if(!test) { // textView.append("\n" +intent.getExtras().get("coordinates")); if (intent.getExtras().containsKey("longitude")) { double longitude = (double) intent.getExtras().get("longitude"); double latitude = (double) intent.getExtras().get("latitude"); googleMap.clear(); setMapView(longitude, latitude); Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault()); try { List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1); if (addressList != null && addressList.size() > 0) { Address address = addressList.get(0); StringBuilder sb = new StringBuilder(); 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()); result.setLatitude(latitude); result.setLongitude(longitude); result.setCountryName(address.getCountryName()); result.setLocality(address.getLocality()); result.setZipCode(address.getPostalCode()); result.setLocation(sb.toString()); if (address.getCountryCode().equals("TW")) { result.setAddress( address.getAddressLine(0).substring(3, address.getAddressLine(0).length())); result.setLocation( address.getAddressLine(0).substring(3, address.getAddressLine(0).length())); } else { result.setAddress(sb.toString()); result.setLocation(sb.toString()); } } } catch (IOException e) { Log.e("", "Unable connect to Geocoder", e); } //test = true; } // } } }; sendDataRequest = new JsonPutsUtil(getActivity()); sendDataRequest.setServerRequestOrderManagerCallBackFunction( new JsonPutsUtil.ServerRequestOrderManagerCallBackFunction() { @Override public void createNormalOrder(NormalOrder order) { if (progressDialog_loading != null) { progressDialog_loading.cancel(); progressDialog_loading = null; } Intent intent = new Intent(getActivity(), ClientTakeRideSearchActivity.class); Bundle b = new Bundle(); b.putInt(Constants.ARG_POSITION, Integer.valueOf(order.getTicket_id())); intent.putExtras(b); startActivity(intent); //finish(); } @Override public void cancelNormalOrder(NormalOrder order) { Intent intent = new Intent(getActivity(), MainActivity.class); startActivity(intent); //finish(); } }); }
From source file:com.tingbacke.wearmaps.MobileActivity.java
/** * Updates my location to currentLocation, moves the camera. * * @param location/*from w w w. ja v a2 s. c om*/ */ private void handleNewLocation(Location location) { Log.d(TAG, location.toString()); double latitude = location.getLatitude(); double longitude = location.getLongitude(); LatLng latLng = new LatLng(latitude, longitude); mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); CameraPosition cameraPosition = new CameraPosition.Builder().target(latLng).zoom(18).bearing(0).tilt(25) .build(); mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); TextView myLatitude = (TextView) findViewById(R.id.textView); TextView myLongitude = (TextView) findViewById(R.id.textView2); TextView myAddress = (TextView) findViewById(R.id.textView3); ImageView myImage = (ImageView) findViewById(R.id.imageView); myLatitude.setText("Latitude: " + String.valueOf(latitude)); myLongitude.setText("Longitude: " + String.valueOf(longitude)); // Instantiating currDistance to get distance to destination from my location currDistance = getDistance(latitude, longitude, destLat, destLong); Geocoder geocoder = new Geocoder(this, Locale.ENGLISH); try { List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); if (addresses != null) { Address returnedAddress = addresses.get(0); StringBuilder strReturnedAddress = new StringBuilder(""); for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) { strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); } // Hardcoded string which searches through strReturnedAddress for specifics // if statements decide which information will be displayed String fake = strReturnedAddress.toString(); if (fake.contains("stra Varvsgatan")) { myAddress.setText("K3, Malm Hgskola" + "\n" + "Cykelparkering: 50m" + "\n" + "Dest: " + Math.round(currDistance) + "m away"); myImage.setImageResource(R.mipmap.kranen); } else if (fake.contains("Lilla Varvsgatan")) { myAddress.setText("Turning Torso" + "\n" + "Caf: 90m " + "\n" + "Dest: " + Math.round(currDistance) + "m away"); myImage.setImageResource(R.mipmap.turning); } else if (fake.contains("Vstra Varvsgatan")) { myAddress.setText("Kockum Fritid" + "\n" + "Bankomat: 47m" + "\n" + "Dest: " + Math.round(currDistance) + "m away"); myImage.setImageResource(R.mipmap.kockum); } else if (fake.contains("Stapelbddsgatan")) { myAddress.setText("Stapelbddsparken" + "\n" + "Toalett: 63m" + "\n" + "Dest: " + Math.round(currDistance) + "m away"); myImage.setImageResource(R.mipmap.stapel); } else if (fake.contains("Stora Varvsgatan")) { myAddress.setText("Media Evolution City" + "\n" + "Busshllplats: 94m" + "\n" + "Dest: " + Math.round(currDistance) + "m away"); myImage.setImageResource(R.mipmap.media); } else if (fake.contains("Masttorget")) { myAddress.setText("Ica Maxi" + "\n" + "Systembolaget: 87m" + "\n" + "Dest: " + Math.round(currDistance) + "m away"); myImage.setImageResource(R.mipmap.ica); } else if (fake.contains("Riggaregatan")) { myAddress.setText("Scaniabadet" + "\n" + "\n" + "Dest: " + Math.round(currDistance) + "m away"); myImage.setImageResource(R.mipmap.scaniabadet); } else if (fake.contains("Dammfrivgen")) { //fakear Turning Torso p min adress fr tillfllet myAddress.setText("Turning Torso" + "\n" + "453m away"); myImage.setImageResource(R.mipmap.turning); } //myAddress.setText(strReturnedAddress.toString()+ "Dest: " + Math.round(currDistance) + "m away"); /* // Added this Toast to display address ---> In order to find out where to call notification builder for wearable Toast.makeText(MobileActivity.this, myAddress.getText().toString(), Toast.LENGTH_LONG).show(); */ //Here is where I call the notification builder for the wearable showNotification(1, "basic", getBasicNotification("myStack")); } else { myAddress.setText("No Address returned!"); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); myAddress.setText("Cannot get Address!"); } }
From source file:eu.trentorise.smartcampus.jp.PlanNewJourneyFragment.java
private void savePosition(Address address, String field) { EditText text = null;/*w ww .j av a 2s . c om*/ ImageButton imgBtn = null; String s = ""; for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) { s += address.getAddressLine(i) + " "; } s = s.trim(); if (field.equals(FROM)) { fromPosition = new Position(address.getAddressLine(0), null, null, Double.toString(address.getLongitude()), Double.toString(address.getLatitude())); if (getView() != null) { text = (EditText) getView().findViewById(R.id.plannew_from_text); imgBtn = (ImageButton) getView().findViewById(R.id.plannew_from_star); } } else if (field.equals(TO)) { toPosition = new Position(address.getAddressLine(0), null, null, Double.toString(address.getLongitude()), Double.toString(address.getLatitude())); if (getView() != null) { text = (EditText) getView().findViewById(R.id.plannew_to_text); imgBtn = (ImageButton) getView().findViewById(R.id.plannew_to_star); } } if (text != null) { text.setFocusable(false); text.setFocusableInTouchMode(false); text.setText(s); text.setFocusable(true); text.setFocusableInTouchMode(true); } if (imgBtn != null) { for (Position p : userPrefsHolder.getFavorites()) { if (address.getAddressLine(0).equalsIgnoreCase(p.getName()) || (address.getLatitude() == Double.parseDouble(p.getLat()) && address.getLongitude() == Double.parseDouble(p.getLon()))) { imgBtn.setImageResource(R.drawable.ic_fav_star_active); imgBtn.setTag(true); } } } }
From source file:info.wncwaterfalls.app.ResultsMapFragment.java
@Override public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) { int count = cursor.getCount(); if (count == 0) { Context context = getActivity(); CharSequence text = "No results found for your search."; int duration = Toast.LENGTH_LONG; Toast toast = Toast.makeText(context, text, duration); toast.show();/*from w w w . j a va 2 s.co m*/ } else { // Put the results on a map! GoogleMap googleMap = mMapView.getMap(); double searchLocationDistanceM = 0; LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder(); if (cursor.moveToFirst()) { // First, add the "searched for" location, if it was a location search. Location originLocation = new Location(""); Address originAddress = sLocationQueryListener.getOriginAddress(); if (originAddress != null) { // Get searched-for distance and convert to meters. short searchLocationDistance = sLocationQueryListener.getSearchLocationDistance(); searchLocationDistanceM = searchLocationDistance * 1609.34; // Build up a list of Address1, Address2, Address3, if present. ArrayList<String> addressList = new ArrayList<String>(); for (int i = 0; i <= 3; i++) { String line = originAddress.getAddressLine(i); if (line != null && line.length() > 0) { addressList.add(line); } } String addressDesc = TextUtils.join("\n", addressList); if (addressDesc == "") { addressDesc = originAddress.getFeatureName(); } if (addressDesc == "") { addressDesc = "Searched Location"; } // Create the LatLng and the map marker. LatLng originLatLng = new LatLng(originAddress.getLatitude(), originAddress.getLongitude()); googleMap.addMarker(new MarkerOptions().position(originLatLng) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)) .title(addressDesc)); boundsBuilder.include(originLatLng); // In case only one result :) // Translate into a Location for distance comparison. originLocation.setLatitude(originAddress.getLatitude()); originLocation.setLongitude(originAddress.getLongitude()); } else { // Not a location search; don't add point for searched-for location // and don't check radius from that point. //Log.d(TAG, "Skipped adding origin address to map."); } // Next, add the results waterfalls. // Use do...while since we're already at the first result. do { // Get some data from the db Long waterfallId = cursor.getLong(AttrDatabase.COLUMNS.indexOf("_id")); double lat = cursor.getDouble(AttrDatabase.COLUMNS.indexOf("geo_lat")); double lon = cursor.getDouble(AttrDatabase.COLUMNS.indexOf("geo_lon")); // Make sure this one's actually within our search radius. SQL only checked // the bounding box. Location waterfallLocation = new Location(""); waterfallLocation.setLatitude(lat); waterfallLocation.setLongitude(lon); if (originAddress == null || (originLocation.distanceTo(waterfallLocation) <= searchLocationDistanceM)) { // Not a location search (originAddress is null: show all) or within radius. // Display on map. String name = cursor.getString(AttrDatabase.COLUMNS.indexOf("name")); LatLng waterfallLatLng = new LatLng(lat, lon); Marker waterfallMarker = googleMap.addMarker(new MarkerOptions().position(waterfallLatLng) .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)) .title(name)); // Save the id so we can retrieve it when clicked mMarkersToIds.put(waterfallMarker, waterfallId); boundsBuilder.include(waterfallLatLng); } } while (cursor.moveToNext()); // Zoom and center the map to bounds mResultBounds = boundsBuilder.build(); zoomToBounds(); } } }
From source file:org.ohmage.reminders.types.location.LocTrigMapsActivity.java
/** * Converts an address to a marker to show on the map * * @param adr/*from ww w.j av a2 s . c o m*/ * @return */ private MarkerOptions addressToMarker(Address adr) { StringBuilder addrText = new StringBuilder(); int addrLines = adr.getMaxAddressLineIndex(); for (int i = 0; i < Math.min(2, addrLines); i++) { addrText.append(adr.getAddressLine(i)).append("\n"); } return new MarkerOptions().position(new LatLng(adr.getLatitude(), adr.getLongitude())) .title(addrText.toString()); }
From source file:com.cecs492a_group4.sp.SingleEvent.java
public String reverseGeocode(double latitude, double longitude) throws IOException { Geocoder gc = new Geocoder(this); if (gc.isPresent()) { List<Address> list = gc.getFromLocation(latitude, longitude, 1); //(latitude, longitude, 1) //33.777043, -118.114395, 1) Address address = list.get(0); StringBuffer str = new StringBuffer(); if (address.getAddressLine(0) != null && address.getLocality() != null && address.getAdminArea() != null && address.getPostalCode() != null && address.getCountryName() != null) { //str.append(address.getAddressLine(0) + ", "); //str.append(address.getLocality() + ", "); //str.append(address.getAdminArea() + " "); //str.append(address.getPostalCode() + ", "); //str.append(address.getCountryName()); //str.append("USA"); //String strAddress = str.toString(); String strAddress = (address.getAddressLine(0) + ", " + address.getLocality() + ", " + address.getAdminArea() + " " + address.getPostalCode() + ", " + "USA"); return strAddress; } else {//from ww w. ja v a2 s .c o m return null; } } return null; }