Pick address when adding marker on map

Asked

Viewed 206 times

2

I would like to know how to get the address when the client adds the marker to the map, and already save it to the firebase server. Thanks 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();

}

1 answer

3

To this end, use the Geocoder.

Here is an example of how to obtain an object Address through a 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();

        }
    }
}

Through the Address, can get the address information.

Example of use:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.