Multiple InfoWindows Android Maps V2

前端 未结 1 1841
我在风中等你
我在风中等你 2021-01-25 03:40

So my application requires me to create a route for a certain bus. I have accomplished this and have created a custom info window for the marker. Now, I was asked to add a featu

相关标签:
1条回答
  • 2021-01-25 04:08

    Got it. So to get multiple Info windows, I had to get the marker ids corresponding to the markers. So I created a ArrayList that takes in the "id" of the marker.

        ArrayList<String> markerPlaces = new ArrayList<>();
    

    I then populated it as I add the markers to the map:

        Marker marker = map.addMarker(new MarkerOptions().position(position)
                            .title(venuesfound.get(i).getName())
                            .snippet("\nOpen: " + venuesfound.get(i).getOpenNow()
                                    + "\n(" + venuesfound.get(i).getCategory() + ")")
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.measle_blue)));
                    markerPlaces.add(marker.getId());        
    

    Then on the InfoAdapter, I just added a condition that if the marker id is in the ArrayList that I created, then put in a different inflater.

        public class InfoAdapter implements GoogleMap.InfoWindowAdapter {
        LayoutInflater inflater = null;
        private TextView textViewstopName;
        private TextView arrivalTime;
        public InfoAdapter(LayoutInflater inflater) {
            this.inflater = inflater;
        }
        @Override
        public View getInfoWindow(Marker marker) {
            if (marker != null) {
                if(markerPlaces.containsKey(marker.getId())) {
                ... //Add new inflater here.
                } //checks if the marker is part of the Position marker or POI marker.
                else{
                    View v = inflater.inflate(R.layout.businfo_layout, null);
                    textViewstopName = (TextView) v.findViewById(R.id.businfo);
                    textViewstopName.setText(marker.getTitle());
                    arrivalTime = (TextView) v.findViewById(R.id.arrivalinfo);
                    arrivalTime.setText(marker.getSnippet());
                    return (v);
                }
            }
            return null;
        }
        @Override
        public View getInfoContents(Marker marker) {
            return (null);
        }
    }
    

    Thank you all for the help! It certainly got me thinking!

    0 讨论(0)
提交回复
热议问题