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

Asked

Viewed 91 times

0

I’ve researched and tried some alternatives, but I haven’t 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 "location a". B) 2 button results in displaying the map only with "location b".

*What I could: link the buttons to the map and display "all" markers. Not desired.

Follow 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);
    }
}

2 answers

1

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

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

//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 the visibility.

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

  • I understood, but how I link to the 2 If that are in the code final part?

  • I don’t understand. What do you mean "how do I link to 2 If"?

  • I want to link the status, visible or not visible, to the button that was selected on the previous screen of the app. And each "If" checks whether the button 1 or 2 has been pressed by the user.

  • Can’t build the if? If you know where it is LocalA.visible(true) and LocalB.visible(false) replace with markerA.visible(true) and markerB.visible(false).

  • I don’t know how to implement the "If" condition check: if(ENTER BY BOOT 1) or if(ENTER BY BOOT 2)

  • You should create another question and add more details so we can understand exactly what you want to do. It seems to me (I’m not sure) that what you need is to implement the method onClick() of each of the buttons.

Show 2 more comments

0


I was able to find out by checking in Ricardo Lecheta’s book, on passing parameters between Activities (screens), follows the solution:

Mainactivity archive:

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 button option passed by parameter - End of 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.

Browser other questions tagged

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