Custom info window for google maps android

孤街浪徒 提交于 2019-12-20 05:14:06

问题


I want to have a custom info window appear on marker click.

I want it to be something like this:

ImageView

TextView

TextView

TextView

Button  Button

I saw other's post question on this, but it's still confusing.


回答1:


About ImageView and TextView, try this:

set an infowindow layout: infowindowlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#FFFFFF" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

In your MapActivity (this is an example for one marker, but you can use this also for others markers):

public class MapActivity extends Activity  {
private GoogleMap mMap;
private Marker Somewhere;
private int markerclicked;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
...
...
final LatLng somewhere = new LatLng(..., ...);

Somewhere=mMap.addMarker(new MarkerOptions()
.position(somewhere)
.title("YOUR TITLE")
.snippet("INFO")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));

...
...
...

mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

        // Use default InfoWindow frame
        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }

        // Defines the contents of the InfoWindow
        @Override
        public View getInfoContents(Marker arg0) {

            // Getting view from the layout file infowindowlayout.xml
            View v = getLayoutInflater().inflate(R.layout.infowindowlayout, null);

            LatLng latLng = arg0.getPosition();

            ImageView im = (ImageView) v.findViewById(R.id.imageView1);
            TextView tv1 = (TextView) v.findViewById(R.id.textView1);
            TextView tv2 = (TextView) v.findViewById(R.id.textView2);
            String title=arg0.getTitle();
            String informations=arg0.getSnippet();

            tv1.setText(title);
            tv2.setText(informations);

            if(onMarkerClick(arg0)==true && markerclicked==1){
               im.setImageResource(R.drawable.yourdrawable);
              }


            return v;

        }
    });
}

public boolean onMarkerClick(final Marker marker) {

if (marker.equals(Somewhere)) 
  {
      markerclicked=1;
      return true;
  }
return false; 
}

About Buttons, ImageButtons, etc. there is no way. The documentation says:

As mentioned in the previous section on info windows, an info window is not a live View, rather the view is rendered as an image onto the map. As a result, any listeners you set on the view are disregarded and you cannot distinguish between click events on various parts of the view. You are advised not to place interactive components — such as buttons, checkboxes, or text inputs — within your custom info window.

Anyway, it seems there is a solution, see this answer here. I don't know if it is applicable with my code, but it seems work with buttons and imagebuttons.



来源:https://stackoverflow.com/questions/24618970/custom-info-window-for-google-maps-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!