How to display marker on the map according to the selected button?

0

I researched and tried some alternatives, but I have not been successful so far.

* I would like to display the markers according to the button pressed on the main screen. For example: A) button 1 results in displaying the map only with "local a". B) button 2 results in displaying the map only with "local b".

* What I did: link the buttons to the map and display "all" the markers. Unwanted.

Here is the short code:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap
;

        // Criando dois locais (A e B :
        LatLng localA = new LatLng(-5.8702316, -35.2079593);
        LatLng localB = new LatLng(-5.8843777, -35.1747881);

        //Inserindo pinos (markers) baseado nos locais criados:
        mMap.addMarker(new MarkerOptions().position(localA)
                .title("Aqui é o local A")//título
                .snippet("Confirme por Tel.: 5555-5555")//subtítulo
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))//cor
                .visible(true));

        mMap.addMarker(new MarkerOptions().position(localB)
                .title("Aqui é o local B”)//título
                .snippet("Confirme por Tel.: 5555-5555")//subtítulo

                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))//cor

                .visible(true)); 

       if(ENTRAR PELO BOTAO 1)

              Exibir o “Local A” e ocultar o “Local B”:

                     LocalA.visible(true)

                     LocalB.visible(false)



       if(ENTRAR PELO BOTAO 2)

              Exibir o “Local B” e ocultar o “Local A”:            

                     LocalA.visible(false)

                     LocalB.visible(true)
        //Local padrão para abertura do mapa:                       mMap.animateCamera(CameraUpdateFactory.newLatLngZoom((localA), 12));//zoo de 12 no local A

        mMap.setMyLocationEnabled(true);
    }
}
    
asked by anonymous 10.07.2018 / 15:10

2 answers

0

I was able to find out by checking in Ricardo Lecheta's book about passing parameters between Activities, the solution follows:

MainActivity File:

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    Button v1 = (Button) findViewById(R.id.button5);

    //Click de um botão qualquer:

    v1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //TROCA DE TELAS:
            Intent intent = new Intent(getApplicationContext(), MapsActivity.class);

            //Passagem de parâmetros para a tela seguinte:
            Bundle params = new Bundle();//declaração
            params.putString("nome", "um");//uso do metodo .putString("chave", "string desejada para ser comparada na tela seguinte")

        //Passagem por parâmetro --> Aqui é onde a mágica acontece:
            intent.putExtras(params);

        //Trocando de tela:
            startActivity(intent);
        }
    });

Screen 2 - MapsActivity.java:

(...)

// Using the option of the button passed by parameter - End of the magic:

    Intent intent = getIntent();
    Bundle args = intent.getExtras();
    String n = args.getString("nome");//capturando a chave para receber a string associada a essa chave.

    String um = "um",
            dois = "dois",
            tres = "tres";

    //Exibindo o pino de acordo com o botao clicado na tela anterior:

    if (n.equals(um)) {
        mMap.addMarker(new MarkerOptions().position(neopolis)
                .title("Nome do local")
                .visible(true)
        //Subtítulo:
                .snippet("Confirme por Tel.: 5555-5555")
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        //Foco do mapa:
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom((neopolis), 12));
    }

Tested and working perfectly on PC and mobile.

    
24.07.2018 / 23:21
1

If you want to control the visibility of Marker after it has been added, you must have a reference to it.

The reference can be obtained at the time the addMarker() method is used.

//Inserindo pinos (markers) baseado nos locais criados:
Marker markerA = mMap.addMarker(new MarkerOptions().position(localA)
        .title("Aqui é o local A")//título
        .snippet("Confirme por Tel.: 5555-5555")//subtítulo
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))//cor
        .visible(true));

Marker markerB mMap.addMarker(new MarkerOptions().position(localB)
        .title("Aqui é o local B”)//título
        .snippet("Confirme por Tel.: 5555-5555")//subtítulo

        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))//cor

        .visible(true)); 

Use markerA.visible() and markerB.visible() to change visibility.

If you want to use them outside of the onMapReady() method, declare them as Activity / Fragment fields

    
10.07.2018 / 15:34