0

I have list of records, where each record has its own latitude and longitude. Based on these lat and long I need to display these records on map as pins.

This is my code to display the records on map:

private GoogleMap mMap;
List<Ticket> tickets = ticketDB.getMyTickets(ticketIDs, mCurrSortKey, mCurrSortOrder, currDate);
    if (mMap != null) {

        LatLngBounds.Builder bounds = new LatLngBounds.Builder();

        for (int i = 0; i < tickets.size(); i++) {
            Ticket ticket = tickets.get(i);
            if (ticket != null && ticket.Latitude > 0) {
                MarkerOptions markerOptions = new MarkerOptions();
                markerOptions.position(new LatLng(ticket.Latitude, ticket.Longitude));
                bounds.include(new LatLng(ticket.Latitude, ticket.Longitude));
                markerOptions.title(ticket.TicketNumber);
                mMap.addMarker(markerOptions);
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(markerOptions.getPosition(),15));
 } 
}

Here, all the ticket records are fetched from database and are successfully marked on map.(Assume there are 10 tickets and so are 10 pins on the marker)

My problem is on clicking on each marker I need to send data of clicked record to open the activity. how to know which record is clicked?

TofferJ
  • 4,678
  • 1
  • 37
  • 49
sai
  • 97
  • 1
  • 10
  • https://stackoverflow.com/questions/14226453/google-maps-api-v2-how-to-make-markers-clickable – Humza Malik Sep 11 '17 at 18:45
  • This might help: https://stackoverflow.com/questions/30601892/android-google-maps-how-to-make-each-marker-infowindow-open-different-activity – Daniel Nugent Sep 11 '17 at 20:56

2 Answers2

0

Implement GoogleMap.OnMarkerClickListener.

@Override
public boolean onMarkerClick(Marker marker) {
    marker.getSnippet();
    marker.getTitle();
    marker.setDraggable(true);
    return false;
}

Here in the given function under marker instance you get everything related to the marker clicked.

0

I solved this by updating google play services to

compile 'com.google.android.gms:play-services:9.4.0'

GoogleMap provides getTag() and setTag() methods in order to do so with the above update.

and implementing OnMapReadyCallback method.

@Override
public void onMapReady(GoogleMap googleMap) {
    GoogleMap mMap = googleMap;
}

add getMapAsync(this) in onCreate().

Now I can set the data to marker using getTag in my above for loop code.

Marker marker = mMap.addMarker(markerOptions);
marker.setTag(ticket);

and can be fetched by:

mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Ticket ticket = (Ticket) marker.getTag();

} }

sai
  • 97
  • 1
  • 10