0

On my Marker I added a setOnMarkerClickListener. So here's my createMethod

protected void createMarker(String driversName, int totalPass, String busNum, double latitude, double longitude, String location) {
    Marker marker = mMap.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude))
            .title(location)
            .snippet("Driver's Name: " + driversName + "\nTotal Passenger: " + totalPass + "\nBus number: " + busNum));
    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
    {
        @Override
        public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker)
        {
            Toast.makeText(MainMapActivity.this, "hey!", Toast.LENGTH_SHORT).show();
            dialogInfo();
            return true;
        }
    });
}

dialogInfo()

public void dialogInfo() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Current Location: "+location);
    builder.setMessage("Bus number: "+busNum+"\nDriver's name: "+name+"\nCurrent total passenger: "+totalPassenger)
            .setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();
}

The values of the dialog is the same

qqrrrr
  • 575
  • 2
  • 8
  • 22
  • Hey @tin Can you explain more in details?? – Mayur Patel Sep 22 '16 at 07:21
  • @mayurpatel Oh, I'm sorry. My fault. I have multiple markers plotted on gMaps. Everytime I clicked on that particular marker, the `busNum`, `name` and `totalPassenger` are just the same. I want to get the particular value for that specific marker. – qqrrrr Sep 22 '16 at 07:29
  • 1
    you want to set custom marker on the map view like Link : http://stackoverflow.com/questions/38560091/add-an-image-for-android-marker-info-window/38560239#38560239 – Mayur Patel Sep 22 '16 at 07:32
  • yeeees. I want a custom marker on the map :) – qqrrrr Sep 22 '16 at 07:33
  • I provide soln at below and more on the above link i has to provided the solution. Please read it – Mayur Patel Sep 22 '16 at 07:37
  • @w4r10ck thanks, I'm checking it right now – qqrrrr Sep 22 '16 at 09:00

1 Answers1

1

Edited

There are few problem in your code, As I can see your intention(please be more specific before asking question).

protected void createMarker(GoogleMap mMap,String driversName, int totalPass, String busNum, double latitude, double longitude, String location) {
        Marker marker = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(latitude, longitude))
                .title(location)
                .snippet("Driver's Name: " + driversName + "\nTotal Passenger: " + totalPass + "\nBus number: " + busNum));
                }

Assign onClickListener outside the method:

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
        {
            @Override
            public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker)
            {
                Toast.makeText(MainMapActivity.this, "hey!", Toast.LENGTH_SHORT).show();
                dialogInfo(marker); //get value from marker
                return true;
            }
        });

dialogInfo()

public void dialogInfo(Marker marker) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(marker.getTitle());
        builder.setMessage(marker.getSnippet())
                .setCancelable(false)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
W4R10CK
  • 5,502
  • 2
  • 19
  • 30