List of usage examples for android.location Address getMaxAddressLineIndex
public int getMaxAddressLineIndex()
From source file:self.yue.vehicletracker.ui.history.HistoryDetailActivity.java
private void getLocationAddress(double latitude, double longitude, final boolean isStartLocation) { new AsyncTask<LatLng, Void, Address>() { @Override//from w w w .j a v a 2 s.c om protected Address doInBackground(LatLng... params) { try { List<Address> addresses = mGeocoder.getFromLocation(params[0].latitude, params[0].longitude, 1); if (addresses != null && addresses.size() > 0) return addresses.get(0); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Address address) { if (address != null) { String addressText = String.format("%s", address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : getString(R.string.unknown_location)); if (isStartLocation) { mTextStartLocation.setText(addressText); } else { mTextEndLocation.setText(addressText); } } else { if (isStartLocation) { mTextStartLocation.setText("Unknown location"); } else { mTextEndLocation.setText("Unknown location"); } } } }.execute(new LatLng(latitude, longitude)); }
From source file:cmu.troy.applogger.AppService.java
private void logApps(List<String> newApps, List<String> currentApps) throws IOException { /*/*from w ww . j a va 2 s . co m*/ * Empty newApps means current apps are the same with the last apps. So there is no need to * update last apps file or log file. */ if (newApps == null || newApps.size() == 0) return; lastApps = currentApps; Date now = new Date(); /* Append new Apps into log file */ JSONObject job = new JSONObject(); String id = String.valueOf(now.getTime()); try { job.put(JSONKeys.id, id); job.put(JSONKeys.first, newApps.get(0)); job.put(JSONKeys.log_type, JSONValues.OPEN_AN_APP); /* Log Location block */ Location mLocation = null; if (mLocationClient.isConnected()) mLocation = mLocationClient.getLastLocation(); if (mLocation != null) { job.put(JSONKeys.loc_available, true); job.put(JSONKeys.latitude, mLocation.getLatitude()); job.put(JSONKeys.longitude, mLocation.getLongitude()); job.put(JSONKeys.location_accuracy, mLocation.getAccuracy()); job.put(JSONKeys.location_updated_time, (new Date(mLocation.getTime())).toString()); Date updateTime = new Date(mLocation.getTime()); if ((updateTime.getTime() - now.getTime()) / 60000 > 5) { Tools.runningLog("Last location is too old, a location update is triggered."); LocationRequest request = new LocationRequest(); request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); mLocationClient.requestLocationUpdates(request, new LocationListener() { @Override public void onLocationChanged(Location arg0) { } }); } if (lastLocation == null || lastAddress == null || lastLocation.distanceTo(mLocation) > Tools.SMALL_DISTANCE) { /* Log Address if location is available */ Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = null; addresses = geocoder.getFromLocation(mLocation.getLatitude(), mLocation.getLongitude(), 1); if (addresses != null && addresses.size() > 0) { job.put(JSONKeys.addr_available, true); Address address = addresses.get(0); lastAddress = address; job.put(JSONKeys.address, address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : ""); job.put(JSONKeys.city, address.getLocality()); job.put(JSONKeys.country, address.getCountryName()); } else { job.put(JSONKeys.addr_available, false); } } else { job.put(JSONKeys.addr_available, true); job.put(JSONKeys.address, lastAddress.getMaxAddressLineIndex() > 0 ? lastAddress.getAddressLine(0) : ""); job.put(JSONKeys.city, lastAddress.getLocality()); job.put(JSONKeys.country, lastAddress.getCountryName()); } lastLocation = mLocation; } else { if (!mLocationClient.isConnecting()) mLocationClient.connect(); job.put(JSONKeys.loc_available, false); } } catch (JSONException e) { Log.e("JSON", e.toString()); } Tools.logJsonNewBlock(job); }
From source file:org.microg.gms.ui.PlacePickerActivity.java
@Override public void onMapEvent(Event event, MapPosition position) { place.viewport = GmsMapsTypeHelper.toLatLngBounds(mapView.map().viewport().getBBox(null, 0)); resultIntent.putExtra(LocationConstants.EXTRA_FINAL_BOUNDS, place.viewport); place.latLng = GmsMapsTypeHelper.toLatLng(position.getGeoPoint()); place.name = ""; place.address = ""; updateInfoText();//from w w w . j a v a2 s . c om if (geocoderInProgress.compareAndSet(false, true)) { new Thread(new Runnable() { @Override public void run() { try { LatLng ll = null; while (ll != place.latLng) { ll = place.latLng; Thread.sleep(1000); } Geocoder geocoder = new Geocoder(PlacePickerActivity.this); List<Address> addresses = geocoder.getFromLocation(place.latLng.latitude, place.latLng.longitude, 1); if (addresses != null && !addresses.isEmpty() && addresses.get(0).getMaxAddressLineIndex() > 0) { Address address = addresses.get(0); StringBuilder sb = new StringBuilder(address.getAddressLine(0)); for (int i = 1; i < address.getMaxAddressLineIndex(); ++i) { if (i == 1 && sb.toString().equals(address.getFeatureName())) { sb = new StringBuilder(address.getAddressLine(i)); continue; } sb.append(", ").append(address.getAddressLine(i)); } if (place.latLng == ll) { place.address = sb.toString(); place.name = address.getFeatureName(); runOnUiThread(new Runnable() { @Override public void run() { updateInfoText(); } }); } } } catch (Exception ignored) { Log.w(TAG, ignored); } finally { geocoderInProgress.lazySet(false); } } }).start(); } }
From source file:group5.trackerexpress.BasicMapActivity.java
protected void makeLatLngMarker(LatLng latLng, String locName, Integer zoom) { String snippet = ""; if (locName == null) { locName = ""; }/* ww w. j av a2 s. co m*/ // https://developer.android.com/training/location/display-address.html 03/04/2015 List<Address> addresses = null; try { addresses = mGeocoder.getFromLocation(latLng.latitude, latLng.longitude, 1); } catch (IOException ioException) { // Catch network or other I/O problems. System.err.println("Geocoder IO EXCEPTION"); } catch (IllegalArgumentException illegalArgumentException) { // Catch invalid latitude or longitude values. System.err.println("Geocoder IllegalArgument at " + latLng.toString()); } if (addresses != null && addresses.size() > 0) { Address address = addresses.get(0); ArrayList<String> addressFragments = new ArrayList<String>(); // Fetch the address lines using getAddressLine, // join them, and send them to the thread. if (address.getMaxAddressLineIndex() > 0) { if (locName.isEmpty()) { System.out.println("Location name is empty"); locName = address.getAddressLine(0); } for (int i = 1; i <= address.getMaxAddressLineIndex(); i++) { addressFragments.add(address.getAddressLine(i)); } } snippet = TextUtils.join(" ", addressFragments); } if (!snippet.isEmpty()) { snippet += System.getProperty("line.separator"); } snippet += latLngFormat(latLng); makeMarker(latLng, locName, snippet); System.out.println("MAKING MARKER"); goToMarker(lastMarker, zoom); }
From source file:org.jraf.android.piclabel.app.form.FormActivity.java
private String reverseGeocode(float lat, float lon) { Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses; try {//w ww.j av a 2 s . c o m addresses = geocoder.getFromLocation(lat, lon, 1); } catch (Throwable t) { Log.w(TAG, "reverseGeocode Could not reverse geocode", t); return null; } if (addresses == null || addresses.isEmpty()) return null; Address address = addresses.get(0); ArrayList<String> strings = new ArrayList<String>(5); if (address.getMaxAddressLineIndex() > 0) strings.add(address.getAddressLine(0)); if (!TextUtils.isEmpty(address.getLocality())) strings.add(address.getLocality()); if (!TextUtils.isEmpty(address.getCountryName())) strings.add(address.getCountryName()); return TextUtils.join(", ", strings); }
From source file:com.cianmcgovern.android.ShopAndShare.Share.java
/** * /*from w w w . j av a 2 s . co m*/ * Gets the address using the latitude and longitude from the getLocation() * */ public void getAddress() { Geocoder geo = new Geocoder(this, Locale.ENGLISH); Address address = null; try { address = geo.getFromLocation(mLocation.getLatitude(), mLocation.getLongitude(), 1).get(0); StringBuilder fullAddress = new StringBuilder(); for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { fullAddress.append(address.getAddressLine(i) + " "); } Log.v(Constants.LOG_TAG, "Got address: " + fullAddress.toString()); mLocationEdit.setText(fullAddress.toString().trim()); } catch (IOException e) { e.printStackTrace(); } mSearch.setBackgroundResource(R.drawable.search); }
From source file:edu.usf.cutr.opentripplanner.android.tasks.OTPGeocoding.java
protected Long doInBackground(String... reqs) { long count = reqs.length; String address = reqs[0];//w w w . j av a 2 s .c o m SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); if (address == null || address.equalsIgnoreCase("")) { return count; } if (address.equalsIgnoreCase(context.getString(R.string.my_location))) { String currentLat = reqs[1]; String currentLng = reqs[2]; LatLng latLng = new LatLng(Double.parseDouble(currentLat), Double.parseDouble(currentLng)); Address addressReturn = new Address(context.getResources().getConfiguration().locale); addressReturn.setLatitude(latLng.latitude); addressReturn.setLongitude(latLng.longitude); addressReturn.setAddressLine(addressReturn.getMaxAddressLineIndex() + 1, context.getString(R.string.my_location)); addressesReturn.add(addressReturn); return count; } ArrayList<Address> addresses = null; if (prefs.getBoolean(OTPApp.PREFERENCE_KEY_USE_ANDROID_GEOCODER, true)) { Log.d("test", "create a geocoder"); Geocoder gc = new Geocoder(context); try { if (selectedServer != null) { addresses = (ArrayList<Address>) gc.getFromLocationName(address, context.getResources().getInteger(R.integer.geocoder_max_results), selectedServer.getLowerLeftLatitude(), selectedServer.getLowerLeftLongitude(), selectedServer.getUpperRightLatitude(), selectedServer.getUpperRightLongitude()); } else { addresses = (ArrayList<Address>) gc.getFromLocationName(address, context.getResources().getInteger(R.integer.geocoder_max_results)); } } catch (IOException e) { e.printStackTrace(); } } addresses = filterAddressesBBox(addresses); if ((addresses == null) || addresses.isEmpty()) { addresses = searchPlaces(address); for (int i = 0; i < addresses.size(); i++) { Address addr = addresses.get(i); String str = addr.getAddressLine(0); List<String> addrLines = Arrays.asList(str.split(", ")); for (int j = 0; j < addrLines.size(); j++) { addr.setAddressLine(j, addrLines.get(j)); } } } addresses = filterAddressesBBox(addresses); addressesReturn.addAll(addresses); return count; }
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;//from w ww.j a v a 2 s . com 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 ww . j a v a2s.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 av a 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(); }