使用osmbonuspack单击MarkerInfoWindow启动意图

时间:2014-09-02 18:05:10

标签: android osmdroid

我有一个GridMarkerClusterer,其中有一些Marker代表共享的自行车站。我希望这些标记具有自定义MarkerInfoWindow(气泡)和点击侦听器,以便当用户点击气泡时,会启动新意图。

到目前为止,我很好。

现在我想把额外的数据(与标记对应的台站信息)放到那个意图上。

我实际做的是在我的StationMarkerInfoWindow中添加一个构造函数,该构造函数在参数中加Station。然后,我使用putExtra()中的OnClickListener将此参数添加到intent中。

那是工作但是错误的是我需要为每个标记创建一个新的StationMarkerInfoWindow而不是使用相同的对象,如果我有超过1000个标记要显示,在我的设备上创建活动最多需要10秒(如果我为每个标记使用相同的StationMarkerInfoWindow对象,则约为1秒。)

问题是:我应该如何将这些数据添加到意图?

以下是代码的相关部分:


public class MapActivity extends Activity {

    private BikeNetwork bikeNetwork;
    private ArrayList<Station> stations;
    private MapView map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        // ...

        stations = bikeNetwork.getStations();

        map = (MapView) findViewById(R.id.mapView);

        GridMarkerClusterer stationsMarkers = new GridMarkerClusterer(this);
        Drawable clusterIconD = getResources().getDrawable(R.drawable.marker_cluster);
        Bitmap clusterIcon = ((BitmapDrawable) clusterIconD).getBitmap();
        map.getOverlays().add(stationsMarkers);
        stationsMarkers.setIcon(clusterIcon);
        stationsMarkers.setGridSize(100);

        for (final Station station : stations) {
            stationsMarkers.add(createStationMarker(station));
        }

        // ...
    }

    private Marker createStationMarker(Station station) {
        Marker marker = new Marker(map);
        marker.setInfoWindow(new StationMarkerInfoWindow(
                R.layout.bonuspack_bubble, map, station)); // this seems wrong
        marker.setInfoWindow(stationMarkerInfoWindow);
        marker.setPosition(stationLocation);
        marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER);
        marker.setIcon(getResources().getDrawable(R.drawable.ic_bike));
        marker.setTitle(station.getName());
        marker.setSnippet(String.valueOf(station.getFreeBikes())); // free bikes
        marker.setSubDescription(String.valueOf(station.getEmptySlots())); // empty slots

        return marker;
    }

    private class StationMarkerInfoWindow extends MarkerInfoWindow {
        Station station;

        public StationMarkerInfoWindow(int layoutResId, final MapView mapView, final Station station) {
            super(layoutResId, mapView);
            this.station = station;
        }

        @Override
        public void onOpen(Object item) {
            super.onOpen(item);
            closeAllInfoWindowsOn(map);

            LinearLayout layout = (LinearLayout) getView().findViewById(R.id.map_bubble_layout);
            layout.setClickable(true);
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(MapActivity.this, StationActivity.class);
                    intent.putExtra("station", station);
                    startActivity(intent);
                }
            });
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我建议将电台设置为&#34;相关对象&#34;其标记:

marker.setRelatedObject(station);

然后,您可以在StationMarkerInfoWindow#onOpen中检索此相关对象:

Marker marker = (Marker)item;
selectedStation = (Station)marker.getRelatedObject();

可以找到类似的实现here

然后你可以共享同一个StationMarkerInfoWindow。