Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set width of custom InfoWindow in Google Maps api v2

I created markers on the map. When I click on them, the custom info windows are always as wide as the screen.

I tried setting layout_width="200dp" but that didn't help. I also tried setting

view.setLayoutParams(new LayoutParams(GetDipsFromPixel(200), GetDipsFromPixel(300)));

I can set the width of the textviews by setting snippetUi.setWidth(GetDipsFromPixel(200)); but the layout still remains screen wide.

<?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"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#FFFFFF"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/worldmap_infowindow_profileimg"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/noimage" />

        <TextView
            android:id="@+id/worldmap_infowindow_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="To do No.2."
            android:textColor="#000000"
            android:textSize="16sp"
            android:textStyle="bold" 
            android:paddingLeft="5dp"/>
    </LinearLayout>

    <View android:id="@+id/separator" 
         android:background="#ababab" 
         android:layout_width = "fill_parent"
         android:layout_marginTop="3dp"
         android:layout_marginBottom="3dp"
         android:layout_height="1dp"/>

    <TextView
        android:id="@+id/worldmap_infowindow_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="See the Giza Pyramids"
        android:textColor="#000000"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/worldmap_infowindow_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="It was fascinating! It's a shame that it was raining, though."
        android:textColor="#000000"
        android:textSize="14sp"
        android:textStyle="normal" />

</LinearLayout>

Class:

 private class CustomInfoWindowAdapter implements InfoWindowAdapter {

       private View view;

       public CustomInfoWindowAdapter() {
            view = getLayoutInflater().inflate(R.layout.custom_infowindow, null);
       }

       @Override
       public View getInfoContents(Marker marker) {

           if (WorldMap.this.myMarker != null
                   && WorldMap.this.myMarker.isInfoWindowShown()) {
               WorldMap.this.myMarker.hideInfoWindow();
               WorldMap.this.myMarker.showInfoWindow();
           }
           return null;
       }

       @Override
       public View getInfoWindow(final Marker marker) {
           WorldMap.this.myMarker = marker;


           final String title = marker.getTitle();
           final TextView titleUi = ((TextView) view.findViewById(R.id.worldmap_infowindow_name));
           if (title != null) {
               titleUi.setText(title);
           } else {
               titleUi.setText("");
           }

           final String snippet = marker.getSnippet();
           final TextView snippetUi = ((TextView) view.findViewById(R.id.worldmap_infowindow_details));
           if (snippet != null) {
               snippetUi.setText(snippet);
           } else {
               snippetUi.setText("");
           }

           return view;
       }
   }

enter image description here

like image 311
erdomester Avatar asked Apr 14 '14 15:04

erdomester


People also ask

How do I change the width of infowindow on Google Maps?

maps. InfoWindow({ content: some_text, maxWidth: 200 }); The documentation notes that the "value is only considered if it is set before a call to open. To change the maximum width when changing content, call close, setOptions, and then open."

How do you change the infowindow position on Google Maps?

An InfoWindow can be placed on a map at a particular position or above a marker, depending on what is specified in the options. Unless auto-pan is disabled, an InfoWindow will pan the map to make itself visible when it is opened. After constructing an InfoWindow, you must call open to display it on the map.

How do I add buttons to infowindow?

event. addListener(marker, 'click', function() { infowindow. setContent('<p>Event Name: '+this. title+'</p>' + '<p>Event Type: '+this.


2 Answers

Basically you just need to have another level of layout below root, like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:background="@android:color/transparent"
android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:orientation="horizontal">
    ...
like image 161
inmyth Avatar answered Oct 01 '22 12:10

inmyth


Just create the parent layout with "wrap_content" and a child layout with the desired size:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical">

    <!-- This layout defines the desired size -->
    <LinearLayout
        android:layout_width="240dp"
        android:layout_height="320dp"
        android:orientation="vertical">

        <!-- Your views here -->

    </LinearLayout>

</LinearLayout>
like image 40
Moisés Moreno Avatar answered Oct 01 '22 12:10

Moisés Moreno