Pick up address when adding bookmark on the map

1

I would like to know how to get the address when the client adds the marker on the map, and already save on the firebase server. Thank you in advance:)

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


    // Add a marker in Sydney and move the camera
    LatLng brazil = new LatLng(-18, -48);


    mMap.setInfoWindowAdapter(new UserAdapter());
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(brazil, 4));


    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    mMap.setOnMapLongClickListener(this);
    UiSettings uiSettings = googleMap.getUiSettings();
    uiSettings.setCompassEnabled(true);
    uiSettings.setZoomControlsEnabled(true);


    mDatabase1.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {
            User childUser = dataSnapshot.getValue(User.class);
            LatLng childPos = new LatLng(childUser.getLat(),childUser.getLang());
            MarkerOptions markerOptions  =new MarkerOptions().position(childPos);
            Marker marker = mMap.addMarker(markerOptions);
            userMarkers.put(dataSnapshot.getKey(),marker);
            userMap.put(dataSnapshot.getKey(),childUser);

        }

        @Override
        public void onChildChanged(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {
            Location childLocation = dataSnapshot.getValue(Location.class);
            Marker oldMarker = userMarkers.get(dataSnapshot.getKey());
            oldMarker.remove();
            LatLng childPos = new LatLng(childLocation.getLat(),childLocation.getLang());
            MarkerOptions markerOptions  =new MarkerOptions().position(childPos);
            Marker marker = mMap.addMarker(markerOptions);
            userMarkers.put(dataSnapshot.getKey(),marker);
            userMap.get(dataSnapshot.getKey()).setLat(childLocation.getLat());
            userMap.get(dataSnapshot.getKey()).setLang(childLocation.getLang());

           //Aqui eu mando para o firebase a possição que foi marcada no mapa
           //quero mandar o endereço por aqui



        }

        @Override
        public void onChildRemoved(com.google.firebase.database.DataSnapshot dataSnapshot) {
            Marker oldMarker = userMarkers.get(dataSnapshot.getKey());
            oldMarker.remove();
            userMarkers.remove(dataSnapshot.getKey());
            userMap.remove(dataSnapshot.getKey());
            //aqui para remover o marcador.
           //Aqui pra remover o endereço.

        }

        @Override
        public void onChildMoved(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }});}



@Override
public void onMapLongClick(LatLng latLng) {
    Location location=new Location(latLng.latitude,latLng.longitude);
    sendMark(location);
}

private void sendMark(Location location){
    String userName= FirebaseAuth.getInstance().getCurrentUser().getUid();
    mDatabase1.child(userName).child("lat").setValue(location.getLat());
    mDatabase1.child(userName).child("lang").setValue(location. getLang());
}

@Override
public void onInfoWindowClick(Marker marker) {
    marker.hideInfoWindow();
    marker.showInfoWindow();

}
    
asked by anonymous 12.03.2017 / 18:02

1 answer

3

To do this, use the GeoCoder .

Here is an example of how to get an object Address through Location :

/**
 * AsyncTask que através do Location pega o endereço
 */
class LoadAddress extends AsyncTask<Location, Void, Address>{


    private Geocoder mGeoCoder;

    @Override
    protected void onPreExecute() {
        mGeoCoder = new Geocoder(getApplicationContext(), Locale.getDefault());
    }

    @Override
    protected Address doInBackground(Location... locations) {
        final Location l = locations[0];
        if(null == l){
            return null;
        }
        List<Address> addresses = null;
        try{
            // vamos pegar apenas 1 endereço
            addresses = mGeoCoder.getFromLocation(l.getLatitude(), l.getLongitude(), 1);

            if(null == addresses || addresses.size() == 0){
                return null;
            }
            final Address _address = addresses.get(0);

            return _address;


        }catch (final Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(String addr) {
        if(null ==addr){
            Toast.makeText(getApplicationContext(), "Não foi possível carregar! ", Toast.LENGTH_SHORT).show();
        }else{
         // Aqui você poderá enviar para a tela, ou fazer o que for necessário!
            Toast.makeText(getApplicationContext(), addr.getAddressLine(0), Toast.LENGTH_SHORT).show();

        }
    }
}

The Address can obtain the address information.

Example usage:

@Override
public void onMapLongClick(LatLng latLng) {
    Location location=new Location(latLng.latitude,latLng.longitude);
    sendMark(location);
   new LoadAddress().execute(location);
}
    
13.03.2017 / 17:18