GoogleMaps marker OnClick - Xamarin - android

Hi everyone I am using the GoogleMaps Gms.Maps class to show a map on my app (Xamarin.Android).
I am trying to make it so when you click on a marker it will take you to a specific page on my mobile app. however it seems the markers already have on click functions that centre it and display the title. I haven't created this and also can't find it (see below)
any idea how I would edit this function or create a new one?
thanks,

Implement Android.Gms.Maps.GoogleMap.IOnMarkerClickListener on your Activity, Fragment or in a separate class and assign that to the GoogleMap instance via the SetOnMarkerClickListener method.
Example:
public class MyMarkerClickListener : Java.Lang.Object, IOnMarkerClickListener
{
Context context;
public MyMarkerClickListener(Context context)
{
this.context = context;
}
public bool OnMarkerClick(Marker marker)
{
Toast.MakeText(context, $"{marker.Title} was tapped", ToastLength.Long).Show();
return true;
}
}
Usage:
googleMap.SetOnMarkerClickListener(new MyMarkerClickListener(this));
Note: Normally the listener would be assigned in the OnMapReady callback
Google Docs: GoogleMap.OnMarkerClickListener
Returns:
true if the listener has consumed the event (i.e., the default behavior should not occur); false otherwise (i.e., the default behavior should occur). The default behavior is for the camera to move to the marker and an info window to appear.

Well what #SushiHangover has answered is right but the easier way of doing it would be adding a marker click event:
Googlemapsobj.MarkerClick+= (s,e)=>
{//yourcodeforNextPage };
Here you wont have to worry about the default behaviour as it will leave the page as soon as it is clicked.

Related

Open a new page when marker clicked on map [Android Studio]

I'm making an Android app on Android Studio (Kotlin) where I have some markers placed on my Google Map and I am confused on how to open a new page/activity when a maker is clicked for more information.
I am aware I need to use an OnClick function but I am confused on how to implement it.
I'm just starting out on Android Studio, so I have been struggling with where to start.
Here how can you use that in Kotlin
mGoogleMap.setOnMarkerClickListener { marker ->
// Your implementation
true
}
GoogleMap has setOnMarkerClickListener interface which has onMarkerClick callback. From there you can call new activity.
mGoogleMap.setOnMarkerClickListener(new OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
// Call your activity here
}
});

Directions and Map default floating button not showing

I want to override the OnMarkerClickListener but the problem is something changed. By default, once a marker is clicked, the directions and map floating button are shown right at the bottom of the map fragment.
After I override the OnMarkerClickListener it changed. Once the marker is clicked it no longer shows the directions and map buttons.
#Override
public boolean onMarkerClick(Marker marker) {
if(previousMarkerClicked == null) {
previousMarkerClicked = marker;
}else{
previousMarkerClicked.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
previousMarkerClicked = marker;
}
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
marker.hideInfoWindow();
markerInfoButton.setVisibility(View.VISIBLE);
selectedMarker_index = getMarkerIndex(marker.getTitle());
return true;
}
How can I show the directions and map button in my map fragment?
Picture that shows Buttons are shown once the marker is clicked
Google docs give this information on how to set the return value of the onMarkerClick() method:
true if the listener has consumed the event (i.e., the default
behavior should not occur); false otherwise (i.e., the default
behavior should occur). The default behavior is for the camera to move
to the marker and an info window to appear.
Basically, this means if you return true (as you are doing) the developer is saying "let me take care of everything". Returning false means that in addition to what the developer instructs inside the method, the system will also do what it usually does.
So if you want the Google Maps Toolbar to appear, you need to return false in the onMarkerClick() method.
NOTE:
You if for some reason you wish to return false in the onMarkerClick() method, but do not want to display the Toolbar you can add this:
mMap.getUISettings().setMapToolbarEnabled(false);

Disable infowindow when click cluster in mapV2

I have referred this https://developers.google.com/maps/documentation/android/utility/marker-clustering link to implement simple cluster in my application
Now I want to disable the cluster onclick
In my screen when I click 3 my info window is showing but I don't want to show infowindow; instead of that, I need to zoom the map.
I have tried
mClusterManager.onInfoWindowClick(null);
mClusterManager.setOnClusterClickListener(null);
mClusterManager.setOnClusterInfoWindowClickListener(null);
mClusterManager.setOnClusterItemClickListener(null);
mClusterManager.setOnClusterItemInfoWindowClickListener(null);
// render.setMarkersToCluster(false);
render.setOnClusterClickListener(null);
render.setOnClusterInfoWindowClickListener(null);
render.setOnClusterItemClickListener(null);
render.setOnClusterItemInfoWindowClickListener(null);
These tries are not at all working; please give me some idea of what to do.
So as we discuss above:
You can use hideInfoWindow() method
Set Renderer from cluster manager as following:
mClusterManager.setRenderer(myClusterRenderer);
Get the marker and hide it as following:
#Override
public void onClusterItemInfoWindowClick(final myPOI item) {
myClusterRenderer.getMarker(item).hideInfoWindow();
}

Android Google Maps V2 Cluster hide infoWindow

In Android project, I'm using Google Maps Android API Utility Librairy in order to use clustering solution.
On each single marker or clustering marker an InfoWindow is opening on a touch on the selected marker. Then on a touch on the infoWindow I use theses events :
...
mClusterManager.setOnClusterInfoWindowClickListener(this);
mClusterManager.setOnClusterItemInfoWindowClickListener(this);
...
and :
#Override
public void onClusterInfoWindowClick(Cluster<JobItem> cluster) {
// Here I go to a new fragment A, list of items.
}
#Override
public void onClusterItemInfoWindowClick(JobItem item) {
// Here I go to a new fragment B, item's details
}
When I come back to the map (popBackStack from fragment A or B) the infoWindow is always open. I would like to hide them programmatically when I go to fragment A or B.
I found it's possible to call methods hideInfoWindow() from markers object but in theses two events markers are not passing through parameters.
Any idea on how to hide the infoWindow ?
I found a solution that work for me, add this code on the onResume of your fragment, it will close all opened InfoWindow.
java.util.Collection<Marker> markerCollection = mClusterManager.getMarkerCollection().getMarkers();
for(Marker m : markerCollection){
if(m.isInfoWindowShown())
m.hideInfoWindow();
}

Android - Maps v2 - How to arrange z-axis of last marker drawn

Ive put together a tool in an app Im building that uses the v2 version of the maps on android and when markers are very close to one another they will draw ontop of one another which isnt a problem but what Id like to have happen at the least is for the last marker drawn to appear on the top.
Is there a way to make sure that the last marker drawn is shown on top of the rest or am I at the mercy of the systems decision making when it comes to showing markers that are to closely grouped together.
For now I just ended up using
marker.showInfoWindow();
It gives me the exact behavior I want which is keeping the last drawn marker to appear on the very top of the other markers on screen.
On a side note it does open the info window but if I really wanted to I could just set a custom layout and make it null for the info window under a certain condition but for my needs it actually works out good that the window opens.
As James already wrote, showing an InfoWindow will do the trick.
You can "show" an invisible InfoWindow by using your own adapter:
public class InfoWindowAdapter implements GoogleMap.InfoWindowAdapter{
private final View mWindow;
public InfoWindowAdapter(Activity activity) {
mWindow = App.activity.getLayoutInflater().inflate(R.layout.custom_info_window,
null);
}
#Override
public View getInfoContents(Marker mark) {
return null;
}
#Override
public View getInfoWindow(Marker marker) {
render(marker, mWindow);
return mWindow;
}
private void render(Marker marker, View view) {
//do nothing
}
}
Set this to the map:
map.setInfoWindowAdapter(new InfoWindowAdapter(activity));
And show the InfoWindow:
currentlySelectedMarker.setSnippet(" ");
currentlySelectedMarker.setTitle(" ");
currentlySelectedMarker.showInfoWindow();
The call of setTitle is important. Otherwise showInfoWindow() will be ignored.

Categories

Resources