How can i detect the button setOnClickListener in info window on google map? Actually in my info window i have a button and i want to click on that button. Is this possible to do this?
问题:
回答1:
Try it
mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
}
});
回答2:
Actually it not posiable, Google Map renders the content of you custom InfoWindow
into an image and displays it to you as an Image. Therefore you set a click Listener only to the whole window and not to the Views inside it.
Your only choice it to set the ClickListener
to the whole InfoWindow
and popup an Dialog with clickable content you want, and not do it directly inside the InfoWindow
.
From Google Docs
: https://developers.google.com/maps/documentation/android/marker#info_windows
Note: The info window that is drawn is not a live view. The view is rendered as an image (using
View.draw(Canvas)
) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (e.g., after an image has loaded), callshowInfoWindow()
. Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.
回答3:
Try this create own class as
public abstract class OnInfoWindowItemTouchListener implements OnTouchListener {
private final View view;
private final int bgDrawableNormal;
private final int bgDrawablePressed;
private final Handler handler = new Handler();
private Marker marker;
private boolean pressed = false;
public OnInfoWindowItemTouchListener(View view, int bgDrawableNormal, int bgDrawablePressed) {
this.view = view;
this.bgDrawableNormal = bgDrawableNormal;
this.bgDrawablePressed = bgDrawablePressed;
}
public void setMarker(Marker marker) {
this.marker = marker;
}
@Override
public boolean onTouch(View vv, MotionEvent event) {
if (0 <= event.getX() && event.getX() <= view.getWidth() &&
0 <= event.getY() && event.getY() <= view.getHeight())
{
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN: startPress(); break;
// We need to delay releasing of the view a little so it shows the pressed state on the screen
case MotionEvent.ACTION_UP: handler.postDelayed(confirmClickRunnable, 150); break;
case MotionEvent.ACTION_CANCEL: endPress(); break;
default: break;
}
}
else {
// If the touch goes outside of the view's area
// (like when moving finger out of the pressed button)
// just release the press
endPress();
}
return true;
}
private void startPress() {
if (!pressed) {
pressed = true;
handler.removeCallbacks(confirmClickRunnable);
view.setBackgroundResource(bgDrawablePressed);
if (marker != null)
marker.showInfoWindow();
}
}
private boolean endPress() {
if (pressed) {
this.pressed = false;
handler.removeCallbacks(confirmClickRunnable);
view.setBackgroundResource(bgDrawableNormal);
if (marker != null)
marker.showInfoWindow();
return true;
}
else
return false;
}
private final Runnable confirmClickRunnable = new Runnable() {
public void run() {
if (endPress()) {
onClickConfirmed(view, marker);
}
}
};
/**
* This is called after a successful click
*/
protected abstract void onClickConfirmed(View v, Marker marker);
}
Use it in your activity:
final OnInfoWindowItemTouchListener buttonListener = new OnInfoWindowItemTouchListener(markHere, 0, 0) {
@Override
protected void onClickConfirmed(View v, Marker marker) {
// more code goes here
}
}
回答4:
We actually tried to address this issue by developing a library the can ease the creation of live(interactive) info windows.
https://github.com/Appolica/InteractiveInfoWindowAndroid
I know it is an old question but I hope it can help someone in the future.
You can basically add your own fragment as an info window using the manager provided by our library. The following is a snipped of how easily the library can be used. Check the link above for more information
final MapInfoWindowFragment mapInfoWindowFragment =
(MapInfoWindowFragment) getSupportFragmentManager().findFragmentById(R.id.infoWindowMap);
final InfoWindow infoWindow = new InfoWindow(marker, markerSpec, fragment);
// Shows the InfoWindow or hides it if it is already opened.
mapInfoWindowFragment.infoWindowManager().toggle(infoWindow, true);