Jared Pawlyzyn Jared Pawlyzyn - 3 years ago 176
Java Question

Android - OnMarkerClick not working

I'm having some troubles with onMarkerClick

Basically for now I just want that when ANY marker previously created starts a toast that says "test". That doesn't seem to be working. I will be implementing the filtering of the marker later, I just want something that allows this to work right now. Thanks for any help in advance. :D

What I get now is no error. Simply nothing happens when the marker is clicked.

Here is my source:

package us.wingitapp.jared.wingit;

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;
PlaceAutocompleteFragment placeAutoComplete;
private static final String TAG = "Main Activity";
List<Marker> list = new ArrayList<>();

private Marker NY_MANHATTAN;

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

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Notice:");
alertDialog.setMessage("Zuya currently only provides information for the greater New York City area, and we need your help expanding! Please consider suggesting exciting new locations.");
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "No Way!",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});

alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Suggest Locations", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://wwww.google.com/"));
startActivity(browserIntent);
}
});

alertDialog.show();

placeAutoComplete = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete);
placeAutoComplete.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {

Log.i(TAG, "Place Selected: " + place.getName());
LatLng latLng = place.getLatLng();

//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 11));
}

@Override
public void onError(Status status) {
Log.d("Maps", "An error occurred: " + status);
}
});

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}

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

try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.style_json));

if (!success) {
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Can't find style. Error: ", e);
}

//TODO: DEFINE MARKER LOCATIONS HERE:

LatLng manhattanLoc = new LatLng(40.754932, -73.984016);
LatLng brooklynLoc = new LatLng(40.650002, -73.949997);
LatLng bronxLoc = new LatLng(40.837048, -73.865433);
LatLng queensLoc = new LatLng(40.742054, -73.769417);
LatLng statenIslandLoc = new LatLng(40.579021, -74.151535);

//TODO: INIT MARKERS HERE:


NY_MANHATTAN = googleMap.addMarker(new MarkerOptions().position(manhattanLoc).title("Manhattan").snippet("test"));
list.add(NY_MANHATTAN);

Marker NY_BROOKLYN = googleMap.addMarker(new MarkerOptions().position(brooklynLoc).title("Brooklyn").snippet("test"));
list.add(NY_BROOKLYN);

Marker NY_BRONX = googleMap.addMarker(new MarkerOptions().position(bronxLoc).title("Bronx").snippet("test"));
list.add(NY_BRONX);

Marker NY_QUEENS = googleMap.addMarker(new MarkerOptions().position(queensLoc).title("Queens").snippet("test"));
list.add(NY_QUEENS);

Marker NY_STATENISLAND = googleMap.addMarker(new MarkerOptions().position(statenIslandLoc).title("Staten Island").snippet("test"));
list.add(NY_STATENISLAND);

googleMap.setOnCameraChangeListener(new
GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
for (Marker m : list) {
m.setVisible(cameraPosition.zoom > 10); // <-- define zoom level for markers here
/**
* Zoom level guidelines:
* 12 = zoomed in about 90%
* 1 = zommed in less than 5%
* Higher = higher zoom, Lower = lower zoom.
* Confusing. I know.
*/
}
}
});


}

public boolean onMarkerClick(Marker arg0) {

Toast.makeText(getApplicationContext(),
"Test", Toast.LENGTH_SHORT)
.show();

return false;
}


}

Answer Source

Initially you have to implement OnMarkerClickListener interface into your Activity and then set it on your map like -

 mMap.setOnMarkerClickListener(this);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download