android listview中的谷歌地图加载问题 [英] Google Map loading issue in android listview

查看:22
本文介绍了android listview中的谷歌地图加载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的列表视图上显示地图视图.地图无法在列表视图中加载.如果我触摸地图视图,地图将加载.如果我滚动列表视图 mapview 进入卸载初始阶段.给出了我的列表视图适配器和屏幕截图,

I am trying to show mapview on my listview. Map cannot load in listview. If i touch the map view, map will load. If i scroll the listview mapview goes to unloaded initial stage. My listview adapter and screenshots are given,

public class OfferListAdapter extends BaseAdapter {

public class OfferListAdapter extends BaseAdapter {

String lat="",lon="";
String adId;
Context context;
MapView mapView;
GoogleMap map; 
Util g;
ArrayList<HashMap<String, String>> adList;
Bundle savedInstanceState;

public OfferListAdapter(final Context context, final ArrayList<HashMap<String, String>> addlist, final String type, final Bundle b) {
    // TODO Auto-generated constructor stub
    this.context=context;
    this.adList=addlist;
    this.listType=type;
    this.savedInstanceState=b;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return adList.size();
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

private class ViewHolder
{
     ImageView adImg;
     TextView txt;
}

@SuppressLint("InflateParams")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    adId=null;
    g=Util.getInstance(context);
    final LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    final View view=mInflater.inflate(R.layout.offer_list_row, null);
    final ViewHolder holder;
    holder=new ViewHolder();
    mapView = (MapView) view.findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);
    // Gets to GoogleMap from the MapView and does initialization stuff
    map = mapView.getMap();
    map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    // Showing / hiding your current location
    map.setMyLocationEnabled(false);
    // Enable / Disable zooming controls
    map.getUiSettings().setZoomControlsEnabled(false);
    // Enable / Disable my location button
    map.getUiSettings().setMyLocationButtonEnabled(false);
    // Enable / Disable Compass icon
    map.getUiSettings().setCompassEnabled(false);
    // Enable / Disable Rotate gesture
    map.getUiSettings().setRotateGesturesEnabled(false);
    // Enable / Disable zooming functionality
    map.getUiSettings().setZoomGesturesEnabled(false);

    MapsInitializer.initialize(context);
    // Updates the location and zoom of the MapView
    double latitude = 12.965119900000000000;
    double longitude = 80.243980900000000000;
    MarkerOptions marker = new MarkerOptions().position(
            new LatLng(latitude, longitude))
            .title("Hello Maps");
    marker.icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_RED));
    map.addMarker(marker);

    //Zoom Particular position
    CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(new LatLng(latitude,
            longitude)).zoom(12).build();

    map.animateCamera(CameraUpdateFactory
            .newCameraPosition(cameraPosition));

    return view;
}

public void updateData(final ArrayList<HashMap<String, String>> addlist, final String type) {
    // TODO Auto-generated method stub
    this.adList=addlist;
    this.listType=type;
}

}

截图列表.1)第一次加载时会出现这个画面,

List of screenshots. 1)This screen will appears in 1st time loading,

2)点击地图视图后会出现这个画面,

2)This screen will appears after a click on mapview,

3)点击地图视图2次后会出现这个画面,

3)This screen will appears after 2 click on mapview,

4) 在地图视图上点击 10 到 15 次后会出现这个画面,

4)This screen will appears after 10 to 15 click on mapview,

推荐答案

在 listView 中直接使用 mapView 是一项繁重的操作.这将导致延迟并导致您的应用程序的用户体验缓慢.为了避免这种行为,您可以选择使用静态地图.这将在您的列表视图中启用地图的延迟加载,并且用户不必时不时地点击地图.

Direct use of the mapView in a listView is a heavy operation. This is going to cause a delay and result in sluggish User Experience of your app. To avoid this behavior you have an alternative to use Static maps. This will enable Lazy Loading of maps in your list view and therby user wont have to tap map now and then.

我在下面提供了一个使用这种方法的小例子.首先从数据(API 或 DB)创建一个列表视图.然后将纬度和经度等数据传递给字符串,其中一些静态变量定义如下.

I am providing a small example below that use this method. First create a list view from the data (Either API or DB). Then pass data such as latitude and longitude to a string with some static variables defined as follows.

String getMapURL = "http://maps.googleapis.com/maps/api/staticmap?zoom=12&size=360x180&markers=size:mid|color:green|"  
+ JOLocation.getString("latitude") 
+ "," 
+ JOLocation.getString("longitude") 
+ "&sensor=false";

上面构造的 URL 在浏览器中使用时,返回一个 .PNG 文件.然后,在我的 Activity 适配器中,此方法会导致延迟加载应用程序中的地图,其中显示坐标的标记是在运行时获取的,从而避免了代码中的繁重工作.

The above constructed URL, when used in a browser, returns a .PNG file. Then, in my adapter for the activity, This method results in the lazy loading of the maps in the app where your marker displaying the coordinates are fetched at runtime, avoiding heavy work in the code.

希望这会有所帮助!!!

Hope this would help!!!

这篇关于android listview中的谷歌地图加载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆