包含在Listview中的Android-MapView
目前,我正在尝试在ListView中放置MapView。有人在这方面取得过成功吗?有可能吗?这是我的密码:包含在Listview中的Android-MapView,android,listview,android-mapview,Android,Listview,Android Mapview,目前,我正在尝试在ListView中放置MapView。有人在这方面取得过成功吗?有可能吗?这是我的密码: ListView myList = (ListView) findViewById(android.R.id.list); List<Map<String, Object>> groupData = new ArrayList<Map<String, Object>>(); Map&l
ListView myList = (ListView) findViewById(android.R.id.list);
List<Map<String, Object>> groupData = new ArrayList<Map<String, Object>>();
Map<String, Object> curGroupMap = new HashMap<String, Object>();
groupData.add(curGroupMap);
curGroupMap.put("ICON", R.drawable.back_icon);
curGroupMap.put("NAME","Go Back");
curGroupMap.put("VALUE","By clicking here");
Iterator it = data.entrySet().iterator();
while (it.hasNext())
{
//Get the key name and value for it
Map.Entry pair = (Map.Entry)it.next();
String keyName = (String) pair.getKey();
String value = pair.getValue().toString();
if (value != null)
{
//Add the parents -- aka main categories
curGroupMap = new HashMap<String, Object>();
groupData.add(curGroupMap);
//Push the correct Icon
if (keyName.equalsIgnoreCase("Phone"))
curGroupMap.put("ICON", R.drawable.phone_icon);
else if (keyName.equalsIgnoreCase("Housing"))
curGroupMap.put("ICON", R.drawable.house_icon);
else if (keyName.equalsIgnoreCase("Website"))
curGroupMap.put("ICON", R.drawable.web_icon);
else if (keyName.equalsIgnoreCase("Area Snapshot"))
curGroupMap.put("ICON", R.drawable.camera_icon);
else if (keyName.equalsIgnoreCase("Overview"))
curGroupMap.put("ICON", R.drawable.overview_icon);
else if (keyName.equalsIgnoreCase("Location"))
curGroupMap.put("ICON", R.drawable.map_icon);
else
curGroupMap.put("ICON", R.drawable.icon);
//Pop on the Name and Value
curGroupMap.put("NAME", keyName);
curGroupMap.put("VALUE", value);
}
}
curGroupMap = new HashMap<String, Object>();
groupData.add(curGroupMap);
curGroupMap.put("ICON", R.drawable.back_icon);
curGroupMap.put("NAME","Go Back");
curGroupMap.put("VALUE","By clicking here");
//Set up adapter
mAdapter = new SimpleAdapter(
mContext,
groupData,
R.layout.exp_list_parent,
new String[] { "ICON", "NAME", "VALUE" },
new int[] { R.id.photoAlbumImg, R.id.rowText1, R.id.rowText2 }
);
myList.setAdapter(mAdapter); //Bind the adapter to the list
ListView myList=(ListView)findviewbyd(android.R.id.list);
List groupData=new ArrayList();
Map curGroupMap=newhashmap();
groupData.add(curGroupMap);
curGroupMap.put(“图标”,R.drawable.back_图标);
curGroupMap.put(“名称”,“返回”);
curGroupMap.put(“值”,“通过单击此处”);
迭代器it=data.entrySet().Iterator();
while(it.hasNext())
{
//获取它的密钥名称和值
Map.Entry对=(Map.Entry)it.next();
String keyName=(String)pair.getKey();
字符串值=pair.getValue().toString();
if(值!=null)
{
//添加父类--也称为主要类别
curGroupMap=newhashmap();
groupData.add(curGroupMap);
//按正确的图标
if(keyName.equalsIgnoreCase(“电话”))
curGroupMap.put(“图标”,R.drawable.phone_图标);
else if(keyName.equalsIgnoreCase(“外壳”))
curGroupMap.put(“图标”,R.drawable.house_图标);
else if(keyName.equalsIgnoreCase(“网站”))
curGroupMap.put(“ICON”,R.drawable.web_ICON);
else if(keyName.equalsIgnoreCase(“区域快照”))
curGroupMap.put(“图标”,R.drawable.camera_图标);
else if(keyName.equalsIgnoreCase(“概述”))
curGroupMap.put(“图标”,R.drawable.overview_图标);
else if(keyName.equalsIgnoreCase(“位置”))
curGroupMap.put(“图标”,R.drawable.map\u图标);
其他的
curGroupMap.put(“ICON”,R.drawable.ICON);
//弹出名称和值
curGroupMap.put(“NAME”,keyName);
curGroupMap.put(“值”,VALUE);
}
}
curGroupMap=newhashmap();
groupData.add(curGroupMap);
curGroupMap.put(“图标”,R.drawable.back_图标);
curGroupMap.put(“名称”,“返回”);
curGroupMap.put(“值”,“通过单击此处”);
//设置适配器
mAdapter=新的simpledapter(
McContext,
分组数据,
R.layout.exp\u列表\u父级,
新字符串[]{“图标”、“名称”、“值”},
新int[]{R.id.photoAlbumImg,R.id.rowText1,R.id.rowText2}
);
myList.setAdapter(mAdapter)//将适配器绑定到列表
提前感谢您的帮助 在这种情况下,您可以像添加任何其他视图一样将MapView添加到列表中。介绍如何创建自定义列表适配器。但我必须提醒你,地图视图是一个相当沉重的视图,如果你试图在屏幕上看到一堆地图视图,你会发现应用程序运行缓慢!您可以在列表项中添加一个按钮,将用户带到另一个包含更多信息(包括地图)的页面。首先,我不认为一次显示多个地图视图会起作用。MapActivity文档每个进程只支持一个: “每个进程只支持一个MapActivity。同时运行的多个MapActivity可能会以意外和不希望的方式进行干扰。” () 它并没有明确地说一个MapActivity中不能有多个地图视图,但我认为它们也会相互干扰,不管它们属于哪种父视图组
第二,您可以考虑使用静态映射API来获得包含在ListVIEW中的简单图像——一个完全成熟的MavVIEW在任何情况下都可能是不必要的重量级:
您可能面临的一个问题是,静态映射API限制了“用户”的使用,这可能意味着通过IP(它不需要API密钥),而移动网络可能存在IP使用限制问题。我不确定这会是怎样的结果。为一个相当老的答案发布一个替代解决方案(实际上超过两年),但我认为这可能会帮助像我一样偶然发现这篇文章的人 注意:对于只需要在“地图”中显示位置,但不需要在
列表视图中与其交互的人来说,这可能很有用。点击列表视图中的一个项目后,实际地图可以显示在详细信息页面上
正如@CaseyB已经指出的,MapView
是一种沉重的视图。为了克服这一点(并让我的生活稍微轻松一点;-),我选择构建一个URL,就像静态Google地图一样,使用我的应用程序所必需的几个参数。您可以在此处获得更多选项:
首先,当我为我的ListView
构建数据时,我将纬度和经度等数据传递给一个字符串,该字符串包含从上述链接中获取的一些静态变量。我从Facebook API获取坐标
我用于构造链接的代码:
String getMapURL = "http://maps.googleapis.com/maps/api/staticmap?zoom=18&size=560x240&markers=size:mid|color:red|"
+ JOLocation.getString("latitude")
+ ","
+ JOLocation.getString("longitude")
+ "&sensor=false";
当在浏览器中使用上述构造的URL时,返回一个.PNG
文件。然后,在活动的适配器中,我使用@Fedor的延迟加载来显示从前面构建的URL生成的图像,以显示在自定义列表视图中。当然,您可以选择自己的方法来显示该地图(实际上是地图的图像)
最后结果的一个例子
目前,我在这个列表视图
中有大约30多个签入地图(我将其与Facebook SDK结合使用),但用户可以有100个签入地图,而且绝对没有关于其速度减慢的报告
考虑到问题已经过去的时间,我怀疑这可能对OP没有帮助,但希望它能帮助其他用户在将来登录此页面。I fa
/**
* This shows to include a map in lite mode in a ListView.
* Note the use of the view holder pattern with the
* {@link com.google.android.gms.maps.OnMapReadyCallback}.
*/
public class LiteListDemoActivity extends AppCompatActivity {
private ListFragment mList;
private MapAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lite_list_demo);
// Set a custom list adapter for a list of locations
mAdapter = new MapAdapter(this, LIST_LOCATIONS);
mList = (ListFragment) getSupportFragmentManager().findFragmentById(R.id.list);
mList.setListAdapter(mAdapter);
// Set a RecyclerListener to clean up MapView from ListView
AbsListView lv = mList.getListView();
lv.setRecyclerListener(mRecycleListener);
}
/**
* Adapter that displays a title and {@link com.google.android.gms.maps.MapView} for each item.
* The layout is defined in <code>lite_list_demo_row.xml</code>. It contains a MapView
* that is programatically initialised in
* {@link #getView(int, android.view.View, android.view.ViewGroup)}
*/
private class MapAdapter extends ArrayAdapter<NamedLocation> {
private final HashSet<MapView> mMaps = new HashSet<MapView>();
public MapAdapter(Context context, NamedLocation[] locations) {
super(context, R.layout.lite_list_demo_row, R.id.lite_listrow_text, locations);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
// Check if a view can be reused, otherwise inflate a layout and set up the view holder
if (row == null) {
// Inflate view from layout file
row = getLayoutInflater().inflate(R.layout.lite_list_demo_row, null);
// Set up holder and assign it to the View
holder = new ViewHolder();
holder.mapView = (MapView) row.findViewById(R.id.lite_listrow_map);
holder.title = (TextView) row.findViewById(R.id.lite_listrow_text);
// Set holder as tag for row for more efficient access.
row.setTag(holder);
// Initialise the MapView
holder.initializeMapView();
// Keep track of MapView
mMaps.add(holder.mapView);
} else {
// View has already been initialised, get its holder
holder = (ViewHolder) row.getTag();
}
// Get the NamedLocation for this item and attach it to the MapView
NamedLocation item = getItem(position);
holder.mapView.setTag(item);
// Ensure the map has been initialised by the on map ready callback in ViewHolder.
// If it is not ready yet, it will be initialised with the NamedLocation set as its tag
// when the callback is received.
if (holder.map != null) {
// The map is already ready to be used
setMapLocation(holder.map, item);
}
// Set the text label for this item
holder.title.setText(item.name);
return row;
}
/**
* Retuns the set of all initialised {@link MapView} objects.
*
* @return All MapViews that have been initialised programmatically by this adapter
*/
public HashSet<MapView> getMaps() {
return mMaps;
}
}
/**
* Displays a {@link LiteListDemoActivity.NamedLocation} on a
* {@link com.google.android.gms.maps.GoogleMap}.
* Adds a marker and centers the camera on the NamedLocation with the normal map type.
*/
private static void setMapLocation(GoogleMap map, NamedLocation data) {
// Add a marker for this item and set the camera
map.moveCamera(CameraUpdateFactory.newLatLngZoom(data.location, 13f));
map.addMarker(new MarkerOptions().position(data.location));
// Set the map type back to normal.
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
/**
* Holder for Views used in the {@link LiteListDemoActivity.MapAdapter}.
* Once the the <code>map</code> field is set, otherwise it is null.
* When the {@link #onMapReady(com.google.android.gms.maps.GoogleMap)} callback is received and
* the {@link com.google.android.gms.maps.GoogleMap} is ready, it stored in the {@link #map}
* field. The map is then initialised with the NamedLocation that is stored as the tag of the
* MapView. This ensures that the map is initialised with the latest data that it should
* display.
*/
class ViewHolder implements OnMapReadyCallback {
MapView mapView;
TextView title;
GoogleMap map;
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getApplicationContext());
map = googleMap;
NamedLocation data = (NamedLocation) mapView.getTag();
if (data != null) {
setMapLocation(map, data);
}
}
/**
* Initialises the MapView by calling its lifecycle methods.
*/
public void initializeMapView() {
if (mapView != null) {
// Initialise the MapView
mapView.onCreate(null);
// Set the map ready callback to receive the GoogleMap object
mapView.getMapAsync(this);
}
}
}
/**
* RecycleListener that completely clears the {@link com.google.android.gms.maps.GoogleMap}
* attached to a row in the ListView.
* Sets the map type to {@link com.google.android.gms.maps.GoogleMap#MAP_TYPE_NONE} and clears
* the map.
*/
private AbsListView.RecyclerListener mRecycleListener = new AbsListView.RecyclerListener() {
@Override
public void onMovedToScrapHeap(View view) {
ViewHolder holder = (ViewHolder) view.getTag();
if (holder != null && holder.map != null) {
// Clear the map and free up resources by changing the map type to none
holder.map.clear();
holder.map.setMapType(GoogleMap.MAP_TYPE_NONE);
}
}
};
/**
* Location represented by a position ({@link com.google.android.gms.maps.model.LatLng} and a
* name ({@link java.lang.String}).
*/
private static class NamedLocation {
public final String name;
public final LatLng location;
NamedLocation(String name, LatLng location) {
this.name = name;
this.location = location;
}
}
}