Directions and Map default floating button not showing - android

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

Related

GoogleMaps marker OnClick - Xamarin

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.

Listener to map touch outside of the markers

I am using the google map api with some markers. I also overrode the infoWindow as below to show custom text.
public void setUpMap() {
final GoogleMap map = mMapView.getMap();
map.clear();
map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
private View mHolder;
#Override
public View getInfoWindow(final Marker marker) {
Log.d("MAP", "Map clicked on marker = " + maker);
etc....
this works fine and I change the icon of each marker when clicked making them visually selected. However, I want to "unselect" all markers. The problem is that I don't know how to add a listener that gets triggered from outside of the markers.
In other words, my listener "getInfoWindow" gets trigger only when a marker is touched. I want the opposite. Some sort of listener that tells me that the user touched the map but not the markers.
Can this be done easily? Any pointers are greatly appreciated.
thx!
Ok, I suppose that's really easy.
You have OnMarkerClickListener and OnMapClickListener.
So, in your case just register OnMapClickListener and in onMapClick() you can do what you need.
One more thing - when you add markers, store them in Arraylist - then at any time you can do whatever is needed - even remove all markers from the map.
This is useful only when working with the Info Window.
In my case, when the user clicks the marker, the corresponding Info window appears. So when the user clicks in the map outside the marker, the Info Window closes and that event is detected by the map.
// Detect when Marker's Info Window is closed
googleMap.setOnInfoWindowCloseListener(new GoogleMap.OnInfoWindowCloseListener() {
#Override
public void onInfoWindowClose(Marker marker) {
// Do whatever you want to do here...
}
});

Display toolbar for Google Maps marker automatically

I have a marker and when I open the map it shows the marker. When I click on it it shows the title and a toolbar which includes two buttons on the bottom right of the map which let me launch intents to navigate to the marker or show it in google maps. I would like to have these displayed automatically when the map is opened rather than having the user to click on the marker.
Like this :-) ...
I can't seem to work out how to do this.
I have tried:
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("title");
// adding marker
googleMap.addMarker(marker).showInfoWindow();
googleMap.getUiSettings().setMapToolbarEnabled(true);
But this just shows the marker title and a button on the top right to go to my location not the two toolbar intent buttons on the bottom right.
Like this :-( ...
I'm a bit stuck any ideas?
The overlay that appears when a marker is clicked, is created and destroyed on-the-spot implicitly. You can't manually show that (yet).
If you must have this functionality, you can create an overlay over your map with 2 ImageViews, and call appropriate intents when they're clicked:
// Directions
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(
"http://maps.google.com/maps?saddr=51.5, 0.125&daddr=51.5, 0.15"));
startActivity(intent);
// Default google map
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(
"http://maps.google.com/maps?q=loc:51.5, 0.125"));
startActivity(intent);
Note: you need to change the coordinates based on Marker's getPosition() and the user's location.
Now to hide the default overlay, all you need to do is return true in the OnMarkerClickListener. Although you'll lose the ability to show InfoWindows and center camera on the marker, you can imitate that simply enough:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
mMap.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
return true;
}
});
I added a feature request here:
http://code.google.com/p/gmaps-api-issues/issues/detail?id=7652
Quoting the doc:
In a lite-mode map, the toolbar persists independently of the user's
actions. In a fully-interactive map, the toolbar slides in when the
user taps a marker and slides out again when the marker is no longer
in focus.
Seeems like there is no way to persist the toolbar in your case since its an interactive map. You might want to try using lite-mode if that is a requirement:
https://developers.google.com/maps/documentation/android/lite
Also looking at your code:
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("title");
// adding marker
googleMap.addMarker(marker).showInfoWindow();
googleMap.getUiSettings().setMapToolbarEnabled(true);
The very last line: googleMap.getUiSettings().setMapToolbarEnabled(true); is redundant unless you are explicitly setting it to false beforehand. The doc states:
Sets the preference for whether the Map Toolbar should be enabled or
disabled. If enabled, users will see a bar with various
context-dependent actions, including 'open this map in the Google Maps
app' and 'find directions to the highlighted marker in the Google Maps
app'.
By default, the Map Toolbar is enabled.
The link: https://developer.android.com/reference/com/google/android/gms/maps/UiSettings.html#setMapToolbarEnabled(boolean)
Hope this helps.
if i'm not mistaken you want to show the two buttons that related to the marker on the right botton side of the map and those buttons has some dependency to the marker if i'm true so you just need tu implement a new method like code below and just call this method on create and sure in your marker click listener
#Override
public void markerIntentButtonShow(Marker marker) {
//setvisible(true) to your side buttons or call some Intent
Intent i = new Intent(getActivity(), MarkerViewDetails.class);
i.putExtra("marker_snippet", marker.getSnippet());
startActivity(i);
}
and call the method in your click listener
googleMap
.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
markerIntentButtonShow(marker);
}
});
and in your onCreateView
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("title");
googleMap.addMarker(marker).showInfoWindow();
markerIntentButtonShow(marker);
I had a similar issue..... if you are okay with not letting the user browse in the map and just focus on your marker, you can use lite-mode map....
Quote from documentation
In a lite-mode map, the toolbar persists independently of the user's actions. In a fully-interactive map, the toolbar slides in when the user taps a marker and slides out again when the marker is no longer in focus.
Again from docs
Also by default when a user taps the map, the API starts the Google Maps mobile app. You can override this by using GoogleMap.setOnMapClickListener() to set your own listener. You can also disable click events on the map, by calling setClickable() on the view that contains the MapView or MapFragment.
For the toolbar you have outlined in red, you can disable it using the setMapToolbarEnabled() method in UISettings
From the documentation:
Sets the preference for whether the Map Toolbar should be enabled or
disabled. If enabled, users will see a bar with various
context-dependent actions, including 'open this map in the Google Maps
app' and 'find directions to the highlighted marker in the Google Maps
app'.
Code example to disable the two buttons:
//Disable Map Toolbar:
mMap.getUiSettings().setMapToolbarEnabled(false);
Just in case you were also wondering about the zoom buttons, you can disable them like this:
mMap.getUiSettings().setZoomControlsEnabled(false);
Show a DialogFragment instead of the info window and handle everything in it.
#Override
public boolean onMarkerClick(Marker marker) {
FragmentManager fm = getSupportFragmentManager();
DialogFragment markerWebView = DialogFragment();
mMap.animateCamera(CameraUpdateFactory.newLatLng(marker.getPosition()));
markerWebView.show(fm, "fragment_marker");
return true;
}
OR
use the marker clicks to turn an overlay on/off using a facsimile of a Google map button
mMap.setOnMarkerClickListener(this);
mMap.setOnInfoWindowClickListener(this);
#Override
public boolean onMarkerClick(Marker marker) {
//here turn on the overlays for nav turns etc. for this marker.
return false
}
#Override
public void onInfoWindowClick(Marker marker) {
//here turn off the overlays for nav turns etc.
marker.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.

Android Google Maps v2 permanent Marker InfoWindow

The default behavior for an InfoWindow is to hide if the map the marker is in, or the marker itself are tapped.
Is there any way to make the InfoWindow permanent so you can't hide it?
I have disabled all gestures in the map, but the InfoWindow still disappears when you tap.
visceralG's solution is what I would do in the first place, but there is also (untested) alternative:
keep a reference to marker showing info window (from getInfoContents)
in onMapClick call markerShowingInfoWindow.showInfoWindow() to force it back
if the above doesn't work, put markerShowingInfoWindow.showInfoWindow() inside Runnable.run() and post it with Handler or View
Not that I'm aware of. You're best bet is to create your own custom icon for the map marker that includes a drawn "infoWindow." It will then require some custom drawing on the infoWindow section of the icon bitmap to produce the same effect as a normal infoWindow, but it's not too terrible to pull off. Not quite as easy as the solution you were looking for, but it'll get you where you were wanting to go. Hope that helps!
Assuming that you already know how to setup info windows, these lines will make your infoWindow, for any one marker, permanent:
final Marker myMarker = mGoogleMap.addMarker(mDriverMarker);
myMarker.showInfoWindow();
mGoogleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
myMarker.showInfoWindow();
}
});
mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
myMarker.showInfoWindow();
return true;
}
});

Categories

Resources