3

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>
jul
  • 36,404
  • 64
  • 191
  • 318

5 Answers5

5

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

I found this library Android Maps Extentions and I used it in my project. It's useful and it can help you easy to pass data into window.

Thanks,

sonida
  • 4,411
  • 1
  • 38
  • 40
0

I solved this by storing my data inside an array, creating a custom layout for the infowindow, and after that passing the indexer of the array after the title so: "title/indexer". Inside the infowindow I split the string and the used the indexer to use the array, and get the data.

JessinR
  • 11
  • 1
-1

Take a look at this code example, there are comments in the code for explanation:

 map.setInfoWindowAdapter(new InfoWindowAdapter() {

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

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

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

                    // Getting the position from the marker
                    clickMarkerLatLng = args.getPosition();

                    //Setting title text
                    TextView title = (TextView) v.findViewById(R.id.tvTitle);
                    title.setText(args.getTitle());

                    map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {          
                        public void onInfoWindowClick(Marker marker) 
                        {
                            if (SGTasksListAppObj.getInstance().currentUserLocation!=null)
                            {   
                                if (String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLatitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) &&
                                        String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLongitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
                                {
                                    Toast.makeText(getApplicationContext(), "This your current location, navigation is not needed.",  Toast.LENGTH_SHORT).show();
                                }
                                else
                                {
                                    FlurryAgent.onEvent("Start navigation window was clicked from daily map");
                                    tasksRepository = SGTasksListAppObj.getInstance().tasksRepository.getTasksRepository();
                                    for (Task tmptask : tasksRepository)
                                    {
                                        String tempTaskLat = String.valueOf(tmptask.getLatitude());
                                        String tempTaskLng = String.valueOf(tmptask.getLongtitude());

                                        Log.d(TAG, String.valueOf(tmptask.getLatitude())+","+String.valueOf(clickMarkerLatLng.latitude).substring(0, 8));

                                        if (tempTaskLat.contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) && tempTaskLng.contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
                                        {  
                                            task = tmptask;
                                            break;
                                        }
                                    }
                                    Intent intent = new Intent(getApplicationContext() ,RoadDirectionsActivity.class);
                                    intent.putExtra(TasksListActivity.KEY_ID, task.getId());
                                    startActivity(intent);

                                }
                            }
                            else
                            {
                                Toast.makeText(getApplicationContext(), "Your current location could not be found,\nNavigation is not possible.",  Toast.LENGTH_SHORT).show();
                            }
                        }
                    });

                    // Returning the view containing InfoWindow contents
                    return v;

                }
            });  

UPDATE:

Then you should associate a HashMap object to your marker, take a look at this answer here:

How to link Google Maps Android API v2 Marker to an object

Community
  • 1
  • 1
Emil Adz
  • 40,709
  • 36
  • 140
  • 187
  • 1
    You're using getPosition() and getTitle(), which are default Marker methods. I need to pass custom data, i.e. in my example an image, a name and an address (I know I could use title and snippet to pass name and address, but I want to be able to add more data). – jul Apr 19 '13 at 15:18
  • take a look at the answer here on how to associate HashMap object to a marker: http://stackoverflow.com/questions/14310280/how-to-link-google-maps-android-api-v2-marker-to-an-object/14324081#14324081 – Emil Adz Apr 19 '13 at 15:32
-1

The below method

mMap.setOnMarkerClickListener(new OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(**Marker arg0**) { } 

helps to pass the value .

ie Marker parameter returns the marker count like 1,2 ,3 etc.

Pass this number like arraylist.get(1).details,you can retrieve the corresponding details

from list

KP_
  • 1,854
  • 25
  • 43