Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data to a custom window info in Google Maps API v2?

I have a custom layout for my map's window info, but I can't see any example of how to pass the data from the marker.

Anybody can help?

mMap.setInfoWindowAdapter(new InfoWindowAdapter() {

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

    @Override
    public View getInfoContents(Marker arg0) {

        View v = getActivity().getLayoutInflater().inflate(R.layout.info_window_layout, null); 

        ImageView pic = (ImageView) v.findViewById(R.id.pic); 
        String url = <????>;
        // set pic image from url

        TextView name = (TextView) v.findViewById(R.id.name); 
        name.setText(<????>);

        TextView address = (TextView) v.findViewById(R.id.address); 
        address.setText(<????>);

        return v;

    }
});

mCenterList = MyApplication.dbHelper.getAllCenter();

for(Center center : mCenterList){

    Marker marker = mMap.addMarker(
            new MarkerOptions().position(new LatLng(center.lat, center.lng))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.elipin)));                

    // How can I pass custom data, here pic, name and address, to the marker, to
    // make it available in getInfoContents?


}

info_window_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="300dp"
    android:layout_height="50dp"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/pic"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginRight="5dp"
        android:scaleType="centerCrop" />

    <ImageView
        android:id="@+id/arrow"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="5dp"
        android:scaleType="centerCrop"
        android:src="@drawable/arrowri" />

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_toRightOf="@id/pic"
        android:layout_toLeftOf="@id/arrow"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:textColor="#ffffff"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/address"
        android:layout_width="match_parent"
        android:layout_height="15dp"
        android:layout_toRightOf="@id/pic"
        android:layout_toLeftOf="@id/arrow"
        android:layout_below="@id/name"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:textColor="#ffffff"
        android:textSize="16sp" />

</RelativeLayout>
like image 758
jul Avatar asked Jan 24 '26 03:01

jul


1 Answers

Since Marker is final, you cannot create a subclass from it.

Hence, the best solution that I have found is to use something in the Marker -- such as the snippet -- to hold a key that allows you to look up additional data. For example, it might be the key to a HashMap of your data model, or it might be the primary key for your database, or something.

Then, your <????> do not come from the Marker directly, but from your real data model, where the Marker just has the identifying information to tie the two together.

like image 60
CommonsWare Avatar answered Jan 26 '26 16:01

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!