Get current coordinate and set it in a text and that does not vary (That shows the one I'm taking at that moment) Android

1

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));
}
    }
}
    
asked by Juan 06.06.2017 в 16:50
source

1 answer

1

Well, I already solved the problem of passing the coordinate and the map is ok. In the previous activity I have a text that shows the coordinates being updated, when I click to see map I send the coordinate by a putExtra to the map, it sends the coordinates of the moment I clicked it so the map loads ok and shows the path, if they go out and re-enter, send that one, that's what I needed because it's not a screen that's always going to be open, it's only out of necessity if you do not know where to go. The only thing I need is this: When I open the activity where the button see map the text takes 1 to 3 seconds to start loading the coordinates, my problem is that if you click on the button before you have loaded it, send the map "Textview" and the map does not break but it does not show the route because I am not passing the origin coordinates. Is there any way that the load of the coordinate is instantaneous or of leaving disabled the button SEE MAP until the textview is not empty?

Solved all I ask and I answer just haha

         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 5000);
    } else {
        locationStart();
    }

private void locationStart() {
    LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Localizacion Local = new Localizacion();
    Local.setMainActivity(this);
    final boolean gpsEnabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (!gpsEnabled) {
        Intent settingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(settingsIntent);
    }
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION,}, 5000);
        return;
    }
    mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, (LocationListener) Local);
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) Local);


}

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 1000) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            locationStart();
            return;
        }
    }
}

/* Aqui empieza la Clase Localizacion */
public class Localizacion implements LocationListener {
    DetallePendiente mainActivity;

    public DetallePendiente getMainActivity() {
        return mainActivity;
    }

    public void setMainActivity(DetallePendiente mainActivity) {
        this.mainActivity = mainActivity;
    }

    @Override
    public void onLocationChanged(Location loc) {
        loc.getLatitude();
        loc.getLongitude();

        // ACA GENERO LA COORDENADA Y LA MUESTRO

        String Text = loc.getLatitude() + "," + loc.getLongitude();
        //Toast.makeText(DetallePendiente.this, Text, Toast.LENGTH_LONG).show();
        origen.setText(Text);

        if(Text != ""){
            btnVerMapa.setEnabled(true);
            btnVerMapa.setText("Ver Mapa");
        }
    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        switch (status) {
            case LocationProvider.AVAILABLE:
                Log.d("debug", "LocationProvider.AVAILABLE");
                break;
            case LocationProvider.OUT_OF_SERVICE:
                Log.d("debug", "LocationProvider.OUT_OF_SERVICE");
                break;
            case LocationProvider.TEMPORARILY_UNAVAILABLE:
                Log.d("debug", "LocationProvider.TEMPORARILY_UNAVAILABLE");
                break;
        }
    }
}

And then I start the VIEW MAP button disabled and with the text loaded:

  btnVerMapa = (Button)findViewById(R.id.btnVerMapa);
    btnVerMapa.setEnabled(false);
    btnVerMapa.setText("Cargando...");

And when the variable text already has the data of the coordinates and it is not empty I do the if to enable the button and change the title to VIEW MAP:

String Text = loc.getLatitude() + "," + loc.getLongitude();


        if(Text != ""){
            btnVerMapa.setEnabled(true);
            btnVerMapa.setText("Ver Mapa");
        }
    
answered by 06.06.2017 / 17:24
source