Skip to content

Instantly share code, notes, and snippets.

@Domacoski
Created January 5, 2018 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Domacoski/2f06ebe6f068411e4c7d9d036cb332f0 to your computer and use it in GitHub Desktop.
Save Domacoski/2f06ebe6f068411e4c7d9d036cb332f0 to your computer and use it in GitHub Desktop.
LOCATIONS
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
public class MapsActivity extends FragmentActivity implements UpdateCarga {
private List<Entrega> entregas = new ArrayList<>(0);
private Location minhaPosicao = null;
private ListView entragasView;
private LoaderAddres loaderAddres = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
minhaPosicao = new Location("MYPosition");
minhaPosicao.setLatitude(-25.380316);
minhaPosicao.setLongitude(-49.273235);
Entrega entrega = new Entrega();
entrega.lat = -25.395773;
entrega.lon = -49.235632;
entregas.add(entrega);
entrega = new Entrega();
entrega.lat = -25.385666;
entrega.lon = -49.233141;
entregas.add(entrega);
entrega = new Entrega();
entrega.lat = -25.362550;
entrega.lon = -49.253930;
entregas.add(entrega);
entragasView = ListView.class.cast(findViewById(R.id.entragasView));
}
@Override
protected void onResume() {
super.onResume();
loaderAddres = new LoaderAddres(entregas, minhaPosicao, this);
loaderAddres.execute();
}
@Override
public void update() {
if (loaderAddres != null) {
entregas = loaderAddres.entregas;
Collections.sort(entregas, new Comparator<Entrega>() {
@Override
public int compare(Entrega o1, Entrega o2) {
final Double aDouble = o2.distancia - o1.distancia;
return aDouble.intValue();
}
});
final EntregaAdapter entregaAdapter = new EntregaAdapter(getApplicationContext());
entragasView.setAdapter(entregaAdapter);
}
}
class EntregaAdapter extends ArrayAdapter<Entrega> {
private final LayoutInflater layoutInflater;
public EntregaAdapter(@NonNull Context context) {
super(context, R.layout.line);
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return entregas.size();
}
@Nullable
@Override
public Entrega getItem(int position) {
return entregas.get(position);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final View view = layoutInflater.inflate(R.layout.line, parent, false);
final Entrega entrega = entregas.get(position);
TextView.class.cast(view.findViewById(R.id.posicao)).setText(String.format("Posição [%d]", position));
TextView.class.cast(view.findViewById(R.id.endereco)).setText(String.format("Endereço: %s", entrega.endereco));
TextView.class.cast(view.findViewById(R.id.distancia)).setText(String.format("Distancia: %s Km", entrega.distancia.toString()));
return view;
}
}
class LoaderAddres extends AsyncTask<Void, Void, Void> {
private final List<Entrega> entregas;
private final Location myLocation;
private final UpdateCarga notifyUpdate;
LoaderAddres(final List<Entrega> entregas, final Location myLocation, final UpdateCarga notifyUpdate) {
this.entregas = entregas;
this.myLocation = myLocation;
this.notifyUpdate = notifyUpdate;
}
@Override
protected Void doInBackground(Void... voids) {
for (final Entrega entrega : entregas) {
entrega.distancia = Utils.distance(myLocation.getLatitude(), myLocation.getLongitude(), entrega.lat, entrega.lon);
entrega.endereco = ConvertPointToLocation(String.valueOf(entrega.lat), String.valueOf(entrega.lon));
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
if (null != notifyUpdate) {
notifyUpdate.update();
}
}
public String ConvertPointToLocation(final String Latitude, final String Longitude) {
StringBuilder addressStr = new StringBuilder();
try {
Geocoder geoCoder = new Geocoder(getApplicationContext(), new Locale("pt", "BR"));
final List<Address> addresses = geoCoder.getFromLocation(Float.parseFloat(Latitude), Float.parseFloat(Longitude), 1);
if (addresses.size() > 0) {
final Address address = addresses.get(0);
// addressStr.append(address.getLocality());
// addressStr.append(" - ");
// addressStr.append(address.getAdminArea());
// addressStr.append(" - ");
// addressStr.append(address.getFeatureName());
// addressStr.append(" - ");
// addressStr.append(address.getPostalCode());
// addressStr.append(" - ");
// addressStr.append(address.getPremises());
// addressStr.append(" - ");
addressStr.append(address.getAddressLine(0));
// addressStr = addresses.get(0).getLocality() + " " + addresses.get(0).getCountryName();
// address = addresses.get(0).getLocality() ;
}
} catch (IOException e) {
e.printStackTrace();
}
return addressStr.toString();
}
}
}
/**
* Created by thiago.domacoski on 05/01/18.
*/
public class Utils {
public static double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1))
* Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1))
* Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return round(dist, 2);
}
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private static double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(value);
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment