我有一个 GridMarkerClusterer
,其中有一些 Marker
代表共享自行车站。我希望这些标记有一个带有点击监听器的自定义 MarkerInfoWindow
(气泡),这样当用户点击气泡时,就会启动一个新的 Intent 。
到目前为止,我还好。
现在我想将额外的数据(与标记对应的站点信息)放入该 Intent 。
我实际上做的是在我的 StationMarkerInfoWindow
中添加一个构造函数,它在参数中接受一个 Station
。然后,我在 OnClickListener
中使用 putExtra()
将此参数添加到 Intent 中。
这是工作,但错误的是我需要为每个标记创建一个新的 StationMarkerInfoWindow
而不是使用相同的对象,如果我有超过 1000 个标记要显示,在我的设备上创建 Activity 最多需要 10 秒(如果我为每个标记使用相同的 StationMarkerInfoWindow
对象,大约需要 1 秒)。
问题是:我应该如何将这些数据添加到 Intent 中?
以下是代码的相关部分:
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);
}
});
}
}
}
最佳答案
我建议将 Station 设置为其标记的“相关对象”:
marker.setRelatedObject(station);
然后您可以在 StationMarkerInfoWindow#onOpen 中检索此相关对象:
Marker marker = (Marker)item;
selectedStation = (Station)marker.getRelatedObject();
可以找到类似的实现 here .
然后您可以共享同一个 StationMarkerInfoWindow。
关于android - 使用 osmbonuspack 启动点击 MarkerInfoWindow 的 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25629583/