MSP
MSP

Reputation: 89

How to open a new activity using onMarkerClick in Android Studio

I am trying to create an Android application in Android Studio, that works with the Google Maps API. I have successfully added the marker to the map to a certain LatLng position I have chosen.
When the marker is clicked, it brings up a title called "Testing". But what I want, is when a user clicks on the markers title, it must open a new activity. However, I can't seem to get it to work. I have added a onMarkerClick and however, I can't implement it. I am really confused. I have tried to add a callback method but not sure how to.
And would you just mind showing me the correct code? Thank you, much appreciated in advance!

package com.msp.googlemapsproject;

import android.content.Intent;
import android.os.Bundle;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends android.support.v4.app.FragmentActivity
        implements GoogleMap.OnMarkerClickListener {

    static final LatLng MyHome = new LatLng(-29.759933, 30.801030);
    private GoogleMap googleMap;

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

        googleMap.setOnMarkerClickListener(this);

        try{

            if (googleMap == null) {

                googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

            }

            googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

            googleMap.setMyLocationEnabled(true);

            googleMap.setTrafficEnabled(true);

            googleMap.setIndoorEnabled(true);

            googleMap.setBuildingsEnabled(true);

            googleMap.getUiSettings().setZoomControlsEnabled(true);

            final Marker MyHome_display = googleMap.addMarker(new MarkerOptions().position(MyHome).title("Testing"));


        } catch (Exception e) {

            e.printStackTrace();
        }
    }

    @Override
    public boolean onMarkerClick(Marker marker) {
        if (marker.equals(MyHome))
        {
            Intent intent = new Intent(MainActivity.this, LastScreen.class);
            startActivity(intent);
        }
        return false;
    }
}

Upvotes: 3

Views: 2993

Answers (4)

Jarin Rocks
Jarin Rocks

Reputation: 1015

$try like this:

public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // Add a marker in Sydney and move the camera
        LatLng chennai = new LatLng(12.9671, 80.2593);
        mMap.addMarker(new MarkerOptions().position(chennai).title("Chennai"));

        LatLng perungudi = new LatLng(12.97, 80.25);
        mMap.addMarker(new MarkerOptions().position(perungudi).title("Perungudi"));

        LatLng pallikarnai = new LatLng(12.9377, 80.2154);
        mMap.addMarker(new MarkerOptions().position(pallikarnai).title("Pallikarnai"));

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(chennai,12));
        mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                if (marker.getTitle().equals("Chennai")){
                    Toast.makeText(MapsActivity.this, "Clicked"+marker.getTitle(), Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });

    }

Upvotes: 0

Zoe - Save the data dump
Zoe - Save the data dump

Reputation: 28238

To switch between activities, use Intent:

Intent intent = new Intent(MainActivity.this, LastScreen.class);
startActivity(intent);

Remember to declare the activity in the manifest, or it will throw an error

Upvotes: 1

Cochi
Cochi

Reputation: 2209

I think your issue is your if condition , because you are testing a Marker and a LatLng, so the condition is always false:

you can try

@Override
    public boolean onMarkerClick(Marker marker) {
        if (marker.getTitle().equals("Testing"))
        {
            Intent intent = new Intent(MainActivity.this, LastScreen.class);
            startActivity(intent);
        }
        return false;
    }

Upvotes: 0

SFAH
SFAH

Reputation: 644

Try this

googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
               Intent intent = new Intent(MainActivity.this, LastScreen.class);
               startActivity(intent);
            }
        });

Upvotes: 1

Related Questions