Open an activity with onMarkerClick

2

I want to open a new activity when I click on the marker, but I click and it is not opening, I did something 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, can you help me?

I did this

@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;
    }

You did not get it, what do I do?

    
asked by anonymous 31.12.2016 / 22:21

2 answers

1

Next, you're first misusing Log.d

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

But about the problem, I'm basically going to explain all the way here, let's just say (maybe someone can explain it more simply, but that's how I can explain it 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 mapping function 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 I insert them into the map, and after I insert the hedge and create a listener, that when clicking on a marker, it will pass receiving treatment there.

In it, I check if the title of the marker is the same as it was before, so I open the Activity

    
02.01.2017 / 18:25
0

I assume that "Point A" and "Point B" are trademark titles. If so, you should use the getTitle () before checking it out.

@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;
}
    
31.12.2016 / 23:10