简体   繁体   中英

How to make google map API markers clickable and open new Activity?

I want to have my markers so that when clicked upon they will open a new activity that will display a list of locations from my SQLite database. If someone could show me the code to add to my markers to open activity for that particular marker it would be great.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

private static final LatLng Kerry = new LatLng(52.212636048, -9.812321255);
private static final LatLng Limerick = new LatLng(52.653131468516236, -8.632676814862014);
private static final LatLng Clare = new LatLng(52.887135246510255, -9.063511195184182);
private static final LatLng Galway = new LatLng(53.27726074573369, -9.065918590802234);
private static final LatLng Mayo = new LatLng(53.88316732870579, -9.42381429549789);
private static final LatLng Sligo = new LatLng(54.20195132022076, -8.59144563453587);
private static final LatLng Leitrim = new LatLng(54.340224636620334, -8.037324331196);
private static final LatLng Donegal = new LatLng(55.013236950319495, -8.226710776524827);
private static final LatLng Cork = new LatLng(51.85961876429461, -8.49543099484229);


private Marker mKerry;
private Marker mLimerick;
private Marker mClare;
private Marker mGalway;
private Marker mMayo;
private Marker mSligo;
private Marker mLeitrim;
private Marker mDonegal;
private Marker mCork;

private LocationManager locationManager;
private LocationListener locationListener;


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

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(@NonNull Location location) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

    ///array list for markers
    List<Marker> markerList = new ArrayList<>();


    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mKerry = googleMap.addMarker(new MarkerOptions()
            .position(Kerry)
            .title("Kerry")
            .snippet("Confirmed cases past 14 days: 44"));
    mKerry.setTag(0);
    markerList.add(mKerry);


    mCork = googleMap.addMarker(new MarkerOptions()
            .position(Cork)
            .title("Cork")
            .snippet("Confirmed cases past 14 days: 191")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mCork.setTag(0);
    markerList.add(mCork);


    mLimerick = googleMap.addMarker(new MarkerOptions()
            .position(Limerick)
            .title("Limerick")
            .snippet("Confirmed cases past 14 days: 270")

            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mLimerick.setTag(0);
    markerList.add(mLimerick);

    mClare = googleMap.addMarker(new MarkerOptions()
            .position(Clare)
            .title("Clare")

            .snippet("Confirmed cases past 14 days: 39")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));

    mClare.setTag(0);
    markerList.add(mClare);


    mGalway = googleMap.addMarker(new MarkerOptions()
            .position(Galway)
            .title("Galway")
            .snippet("Confirmed cases past 14 days: 143")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mGalway.setTag(0);
    markerList.add(mGalway);



    mMayo = googleMap.addMarker(new MarkerOptions()
            .position(Mayo)
            .title("Mayo")
            .snippet("Confirmed cases past 14 days: 98")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mMayo.setTag(0);
    markerList.add(mMayo);


    mSligo = googleMap.addMarker(new MarkerOptions()
            .position(Sligo)
            .title("Sligo")
            .snippet("Confirmed cases past 14 days: 20")

            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mSligo.setTag(0);
    markerList.add(mSligo);


    mLeitrim = googleMap.addMarker(new MarkerOptions()
            .position(Leitrim)
            .title("Leitrim")
            .snippet("Confirmed cases past 14 days: 5")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mLeitrim.setTag(0);
    markerList.add(mLeitrim);


    mDonegal = googleMap.addMarker(new MarkerOptions()
            .position(Donegal)
            .title("Donegal")
            .snippet("Confirmed cases past 14 days: 351")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    mDonegal.setTag(0);
    markerList.add(mDonegal);

    for (Marker m : markerList) {
        LatLng latLng = new LatLng(m.getPosition().latitude, m.getPosition().longitude);
        mMap.addMarker(new MarkerOptions().position(latLng));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 7));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 7));



    }



}

}

'''

I think it's something like:

mMap.setOnInfoWindowClickListener(marker -> {
    Intent intent = new Intent(this, SomeActivity.class);
    intent.putExtra("Key", marker.getTag().toString());
    startActivity(intent);
});

Somewhere in SomeActivity, to retrieve the string tag of the marker:

String value = getIntent().getParcelableExtra("Key");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM