Open an Activity with onMarkerClick

Asked

Viewed 103 times

2

i want to open a new Activity when clicking on the Marketer, however I click and it is not opening, I did more or less like this:

@Override
public boolean onMarkerClick (final Marker marker){
    if (marker.equals("Ponto A")){
        Intent i = new Intent(Mapa.this, Tela1.class);
        startActivity(i);
    }else if (marker.equals("Ponto B")){
        Intent i = new Intent(Mapa.this, Tela2.class);
        startActivity(i);
    }
    return true;
}

But it’s not open yet, you can help me??

i did it

@Override
    public boolean onMarkerClick(final Marker marker) {
        if (marker.getTitle().equals(Log.d("onMarkerClick","Ponto A"))) {
            Intent i= new Intent(Maps.this, Tela1.class);
            startActivity(i);
        }else if (marker.getTitle().equals(Log.d("onMarkerClick","Ponto B"))) {
            Intent i= new Intent(Maps.this, Tela2.class);
            startActivity(i);
        }
        return true;
    }

That’s not how I do it?

  • If you don’t put any if, it opens an Activity?

  • 1

    but if I didn’t put any if, which Mark it would read?

  • Any Marker, however this is good to test to see if it is getting to that part of the code, so we can check if the error is in ifs, or in outar part

  • yes, it is, I will take the ifs and by direct, man thanks!!!!

  • I took out the ifs and it didn’t work either...

2 answers

1


Next, first you are misusing the Log. d

It is used to print in the logcat some information for you to check, and not as a function that will give you a return.

But about the problem, I’ll explain basically all the way here, in a simple way let’s say (someone may be able to explain in a simpler way, but it’s how I can explain at the moment).

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapa_fragment);
mapFragment.getMapAsync(new OnMapReadyCallback() {
    @Override
    public void onMapReady(GoogleMap googleMap) {
        // Cria o marker do ponto A e seta no mapa
        MarkerOptions markerA = new MarkerOptions();
        markerA.position(new LatLng(-10.0,-10.0));
        markerA.title("Ponto A");
        markerA.icon(BitmapDescriptorFactory.defaultMarker());
        googleMap.addMarker(markerA);
        // Cria o marker do ponto B e seta no mapa
        MarkerOptions markerB = new MarkerOptions();
        markerB.position(new LatLng(10.0,10.0));
        markerB.title("Ponto B");
        markerB.icon(BitmapDescriptorFactory.defaultMarker());
        googleMap.addMarker(markerB);

        // Listener para quando clicar no mapa
        googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                if(marker.getTitle().equals("Ponto A")){
                    // ABRE ACTIVITY PONTO A
                }else if(marker.getTitle().equals("Ponto B")){
                    // ABRE ACTIVITY PONTO B
                }
                return true;
            }
        });
    }
});

In this code I call the function of taking the map asynchronously, so that I can work with the map and create a callback for when the map is ready.

When the map is ready, I create 2 markers, and insert them into the map, and after inserted I serve and create a Systener, which by clicking a Marketer, it will pass receiving treatment there.

in it I check if the title of Marker is the same as the one I set up earlier, and so I open the Activity

  • worked perfectly!!!

  • 1

    The example was made so to be easy to understand, if you want to work with maps later, better save the class of googlemaps in a variable of the same class. I’m happy to have helped you :)

  • 1

    Yes, yes, I did.. It was just this doubt that I had about opening an Activity because I got a little complicated. But man thank you very much and thanks for the patience. You are too !

0

I suppose "Point A" and "Point B" are brand titles.
If so, use the method getTitle() before making the verification.

@Override
public boolean onMarkerClick (final Marker marker){
    if (marker.getTitle().equals("Ponto A")){
        Intent i = new Intent(Mapa.this, Tela1.class);
        startActivity(i);
    }else if (marker.getTitle().equals("Ponto B")){
        Intent i = new Intent(Mapa.this, Tela2.class);
        startActivity(i);
    }
    return true;
}
  • Dude, you’re still not getting it, I wonder if I should log?

  • Put a "log" before the first if and one more after each if to indicate where the execution goes after clicking on the tag.

  • can exemplify?

  • 1

    Use 3 Log.d("onMarkerClick", "texto");. Substitute "texto" for "antes do if" in the first, "if Ponto A" in the second and "if Ponto B" in the third.

  • I wrote again with Log but it didn’t take

  • 1

    Behold here how to use the logcat to "debug" the application.

  • I made the code that way, there’s something wrong?

  • If you don’t even know how to use the logcat it is difficult to help you. If my answer has not solved the problem should not mark it as correct (uncheck V). However read the content of the link I indicated.

  • Okay, thank you very much and forgive me for my ignorance. hugs!!

Show 4 more comments

Browser other questions tagged

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