Good morning: I have the following code that makes a map and puts two points on the same point A Start of the route I wrote it in the variable, point B End of the route I bring it from the database and I pass it from a previous activity for a putExtra. Finally between the two points I trace the route. What I need is that the starting point is my current location, either coordinates or direction, either. Probe obtaining the coordinates and shows me the two points, it happens that the coordinates remain as in a cache (I suppose) because it does not matter where I go and reopen the map always the origin is the address that I take the first time when I probe it ( I do not need to update constantly I need that when I click on the previous activity VIEW MAP enter this activity and show me the current location as point of ORIGIN). Then try to change the coordinates and achieve it but as the location of the GPS changes constantly the map is loading because the ORIGIN coordinates are changing every time. That is the explanation. My question is how do I put my current location in this line:
etOrigin.setText("ACA VA MI POSICION ACTUAL, COORDENADAS, DIRECCION, CUALQUIER FORMATO");
I have to generate it on the previous page and pass it with an inutExtra? Or can I generate it in the same activity on the map and every time I open it, I set the coordinate that I took in that time without changing so that the map loads well?
Here's the MapsActivity code:
private GoogleMap mMap;
private EditText etOrigin;
private EditText etDestination;
private List<Marker>originMarkers = new ArrayList<>();
private List<Marker>destinationMarkers = new ArrayList<>();
private List<Polyline>polylinePaths = new ArrayList<>();
private ProgressDialog progressDialog;
private String p = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
etOrigin = (EditText) findViewById(R.id.etOrigin);
etDestination = (EditText) findViewById(R.id.etDestination);
Intent intent = getIntent();
final Bundle extras = intent.getExtras();
if (extras != null) {
p = extras.getString("direccion");
etDestination.setText(p);
}
etOrigin.setText("ACA VA MI POSICION ACTUAL, COORDENADAS, DIRECCION, CUALQUIER FORMATO");
sendRequest();
}
private void sendRequest() {
String origin = etOrigin.getText().toString();
String destination = etDestination.getText().toString();
if (origin.isEmpty()) {
Toast.makeText(this, "Por favor ponga una dirección de inicio", Toast.LENGTH_SHORT).show();
return;
}
if (destination.isEmpty()) {
Toast.makeText(this, "Por favor ponga una dirección de destino", Toast.LENGTH_SHORT).show();
return;
}
try {
new DirectionFinder(this, origin, destination).execute();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
return;
}
mMap.getUiSettings().setZoomControlsEnabled(true);
}
@Override
public void onDirectionFinderStart() {
progressDialog = ProgressDialog.show(this, "Por favor espere...",
"Buscando las direcciones...", true);
// SI ALGUNO DE LOS CAMPOS ESTA VACIO NO LLEGA AL MENSAJE
// SOLO REMUEVE LOS MARKETS Y REINICIA
if (originMarkers != null) {
for (Marker marker : originMarkers) {
marker.remove();
}
}
if (destinationMarkers != null) {
for (Marker marker : destinationMarkers) {
marker.remove();
}
}
if (polylinePaths != null) {
for (Polyline polyline:polylinePaths ) {
polyline.remove();
}
}
}
@Override
public void onDirectionFinderSuccess(List<Route> routes) {
progressDialog.dismiss();
polylinePaths = new ArrayList<>();
originMarkers = new ArrayList<>();
destinationMarkers = new ArrayList<>();
for (Route route : routes) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 15));
originMarkers.add(mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue))
.title(route.startAddress)
.position(route.startLocation)));
destinationMarkers.add(mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green))
.title(route.endAddress)
.position(route.endLocation)));
PolylineOptions polylineOptions = new PolylineOptions().
geodesic(true).
color(Color.BLUE).
width(10);
for (int i = 0; i < route.points.size(); i++)
polylineOptions.add(route.points.get(i));
polylinePaths.add(mMap.addPolyline(polylineOptions));
}
}
}