如何将ArrayList中的动态数据设置为InfoWindow

时间:2014-10-16 16:08:39

标签: android arraylist google-maps-android-api-2 infowindow

我在使用动态内容填充InfoWindows时遇到了困难。我有一个包含数据的ArrayList,我想填充infoWindow。如果我在标记上使用方法片段,动态显示数据,但所有在一行,所以我被迫使用CustomInfoWindowAdapter。如果我使用CustomInfoWindowAdapter,我会为每个InfoWindow显示相同的数据。我错了什么?

这是我的CustomInfoWindowAdapter:

class MarkerInfoWindowAdapter implements InfoWindowAdapter {

        private View inflatedView;
        private View tempView;
        private String title, description;

        public void setTitle(String title) {
            this.title = title;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        MarkerInfoWindowAdapter() {
            inflatedView = getLayoutInflater().inflate(R.layout.info_window_layout, null);
         //   tempView = getLayoutInflater().inflate(R.layout.location_content_window, null);
        }

        @Override
        public View getInfoContents(Marker marker) {
          //  setInfo(marker, inflatedView);
            return inflatedView;
        }

        @Override
        public View getInfoWindow(Marker marker) {
           setInfo(marker, inflatedView);
            return inflatedView;
        }

        private void setInfo(Marker marker, View view) {
            final TextView txtTitle = (TextView) view.findViewById(R.id.title);
            final TextView txtDescription = (TextView) view.findViewById(R.id.lat_lng);
            txtTitle.setText(title);
            txtDescription.setText(description);
        }
    } 

这是我从ArrayList创建Marker和InfoWindow的方法。

public void drawRoute(HashMap<String, ArrayList<HashMap<String, String>>> result, String type) {
        ArrayList<HashMap<String, String>> voyageplan = result.get("optimal");
        HashMap<String, String> waypoint1;

        for (int i = 0; i < voyageplan.size(); i++) {

            waypoint1 = voyageplan.get(i);        

            googleMap.addMarker(new MarkerOptions().position(position)
             .title("WP# "+waypoint1.get("WP").toString()+" (optimal)")
             .snippet("prova")
             .anchor(0.5f, 0.5f).draggable(false)    
             .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));

            infoWindowAdapter = new MarkerInfoWindowAdapter();
            infoWindowAdapter.setTitle(voyageplan.get(i).get("WP"));
            infoWindowAdapter.setDescription(voyageplan.get(i).get("lat"));
            googleMap.setInfoWindowAdapter(infoWindowAdapter);
    }

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我不知道如何修复你的方法,但这就是我实现我的方法。我实际上有两种方法。

第一种方法:创建另一个函数来为您获取数据 并在mapInitialization中调用该函数。

第二种方法:保存每个marker.snippet中的所有动态数据 属性为逗号分隔值并提取您的信息 通过处理分离这些以逗号分隔的存储数据 对于每个标记。

我实现了下面的第一种方法。

// This function is where you read from your array, web, REST API or
// whatever to get the dynamic  data needed to put into your markers. 
//I just used a simple case of if and else to show dynamically changing
//string is returned. 

private String getDescriptionFromMarkerSoon(double latitude, double longitude) {
    // Access database or data structure here using arguments as key
    if (longitude > 0) {
        return "I am a boy";
    } else {
        return "I am a girl";
    }
}

// This is the one time setup that you need to do inside the setUpMapIfNeeded()
// function that is created by Android Studio if you initialize your project
// as a Google Maps Template. 
private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the referecen of the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            // add the layout for customizedInfoWindow in Google Maps
            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

                @Override
                // Returning null here will automatically call getInfoContents
                public View getInfoWindow(Marker arg0) {
                    return null;
                }

                // Get the information to be displayed on the current window
                @Override
                public View getInfoContents(Marker marker) {
                    // Get the layout file for InfoContent
                    View v = getLayoutInflater().inflate(R.layout.info_window, null); // null means dont attach to any parent window
                    TextView tvDescription = (TextView) v.findViewById(R.id.tv_description);
                    TextView tvSnippet = (TextView) v.findViewById(R.id.tv_snippet);
                    TextView tvLat = (TextView) v.findViewById(R.id.tv_lat);
                    TextView tvLng = (TextView) v.findViewById(R.id.tv_lng);

                    // Get position of marker
                    LatLng markerPos = marker.getPosition();
                    tvLat.setText("Latitude: " + markerPos.latitude);
                    tvLng.setText("Longitude: " + markerPos.longitude);
                    tvSnippet.setText(marker.getSnippet());
                   // Call the function you created here to get
                   // the dynamic data 
                   tvDescription.setText(getDescriptionFromMarkerSoon
                   (markerPos.latitude, markerPos.longitude));
                    // Return the view created
                    return v;
                }
            });
            setUpMap();

        }
    }
}

这就是我的layout / info_window.xml文件在这个例子中的样子

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

    <TextView
        android:id="@+id/tv_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:textStyle="bold"
        />


    <TextView android:id="@+id/tv_snippet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="10sp"
        />

    <TextView
        android:id="@+id/tv_lat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="5sp"

        />

    <TextView android:id="@+id/tv_lng"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="5sp"

        />

    </LinearLayout>
</LinearLayout>