Retrieve data from a map in another activity - Android Studio

0

Hello! I have a class called Maps, with which I can know the location of the phone:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

Button siguiente;
TextView messageTextView;
private GoogleMap mMap;
private Marker marcador;
double lat = 0.0;
double lng = 0.0;

@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);


}

@Override
protected void onResume() {
    super.onResume();
    siguiente = (Button) findViewById(R.id.button_next_services);

    siguiente.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent siguiente = new Intent(MapsActivity.this, SelectServicesActivity.class);
            startActivity(siguiente);
        }
    });
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    miUbicacion();
}

private void agregarMarcador(double lat, double lng) {
    LatLng coordenadas = new LatLng(lat, lng);
    CameraUpdate miUbicacion = CameraUpdateFactory.newLatLngZoom(coordenadas, 16);
    if (marcador != null) marcador.remove();
    marcador = mMap.addMarker(new MarkerOptions()
            .position(coordenadas)
            .draggable(true)
            .title("Mi posición actual")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
    mMap.animateCamera(miUbicacion);
}

private void actualizarUbicacion(Location location) {
    if (location != null) {
        lat = location.getLatitude();
        lng = location.getLongitude();
        agregarMarcador(lat, lng);

    }
}

LocationListener locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        actualizarUbicacion(location);

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
};

private void miUbicacion() {

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {


        return;
    }
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Location location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
    actualizarUbicacion(location);
    locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER,15000,0,locationListener);

}

}

However, it only shows you the position marker, how can I get the address of the marker as such and retrieve it in another activity?

Thanks for your help!

    
asked by Odalis T 24.05.2017 в 03:17
source

1 answer

0

Once you have the desired latitude and longitude, you must use a geocoder:

Geocoder mGeocoder = new Geocoder(context);
List<Address> lista = mGeocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 5);

Where list will have up to 5 results (the last parameter is the number of results you want). Address objects within the list have street, number, status, etc. You can form your own strings with that information and pass them as extras to your next Activity, or make your own parcel objects to achieve it.

Reference: link

    
answered by 24.05.2017 в 03:59