简体   繁体   中英

Why can't I access a button added to a custom info window added to a marker in Google Maps?

I'm creating a small app that will help a user find a sports club. Based on the type of sport picked from a spinner in the previous activity a number of markers representing different clubs will be shown on a map.(See screenshot below)

Results after a type of sport is clicked in a spinner

I've attempted to add a button to an information window (contains a title, and a snippet) of a marker in Google Maps that will start another activity. It will produce a toast pop up for now. At the moment I'm producing the following result:

App Screenshot (unable to embed image as reputation too low)

The issue I'm having is that I cannot interact with the button in the info window when I try to click the button the whole information window gets clicked instead (leads to the window changing from a white to a grey background to show it was clicked). This happens whether I try to click the button or any other section of the information window.

Grey background to signify info window was clicked

To do this I created an XML layout file (custom_window_info.xml) that would be displayed in the map activity in the event of a marker being clicked. It displays the title, snippet and the button method declared in the MapActivity.java class.

I then created a java class (CustomInfoWindowAdapter.java) that provided the java functionality for the layout file mentioned above, eg assigning the values to the title and snippet text fields, the getInfoWindow() and getInfoContents() methods, etc.

I'm not sure If I've approached this the wrong way entirely or I'm making a little mistake. Any tips/suggestions?

custom_window_info.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/title"
                android:layout_gravity="center_horizontal"
                android:ellipsize="end"
                android:maxLines="1"
                android:textColor="#000"
                android:textSize="14sp"
                android:textStyle="bold"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:id="@+id/snippet"
                android:maxLines="5" />

            <Button
                android:id="@+id/joinCLub"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:onClick="ClubButtonClick"
                android:text="Join Club"
                android:visibility="visible" />
        </LinearLayout>
    </LinearLayout>

CustomInfoWindowAdapter.java

public class CustomInfoWindowAdapter implements GoogleMap.InfoWindowAdapter {

private final View mWindow;
private Context mContext;

public CustomInfoWindowAdapter(Context context) {
    this.mContext = mContext;
    this.mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window, null);
}

private void rendowWindowText(Marker marker, View v){

    String title = marker.getTitle();
    TextView clubtitle = (TextView) v.findViewById(R.id.title);

    if(!title.equals(" ")){
        clubtitle.setText(title);
    }

    String snippet = marker.getSnippet();
    TextView clubsnippet = (TextView) v.findViewById(R.id.snippet);

    if(!title.equals(" ")){
        clubsnippet.setText(snippet);
    }
}

@Override
public View getInfoWindow(Marker marker) {
    rendowWindowText(marker, mWindow);
    return mWindow;
}

@Override
public View getInfoContents(Marker marker) {
    rendowWindowText(marker, mWindow);
    return mWindow;
}


  }

MapActivity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

  private GoogleMap mMap;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
  }


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

    // Assigning coordinates to a club (I've left out the 4 other markers to reduce the code)
    LatLng vincentsGAA = new LatLng(53.374042,-6.2300634);

    // Adding marker to map with the coordinates declared above, a title for the name of the club and a snippet for the address  
    mMap.addMarker(new MarkerOptions().position(vincentsGAA).title("St Vincents GFC").snippet("Malahide Road, Dublin"));

    //Assigning the default coordinates to Dublin, Ireland when the map opens
    LatLng dublin = new LatLng(53.3242381,-6.385785);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dublin, 10));

    // Sets the custom info window to this map activity's markers
    mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter(MapsActivity.this));
  }


  // This is the back button in the top right of the map activity that will 
  // bring the user back to the previous activity that had the spinner that 
  // allows the user to pick the type of sport they want to see clubs for
  public void ButtonClick(View v) {


     Intent intent1 = new Intent(this, Welcome_Activity.class);
     startActivity(intent1);

  }
}

Actually, it not possible, Google Map renders the content of your 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 is to set the ClickListener to the whole InfoWindow and popup a 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 (eg, after an image has loaded), call showInfoWindow(). 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.`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM