在Android的ListView控件谷歌地图加载问题 [英] Google Map loading issue in android listview

查看:300
本文介绍了在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,

公共类OfferListAdapter延伸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)该屏幕会显示在第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点击出现在MapView类,

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

推荐答案

直接用在ListView的MapView的是一个沉重的操作。这将导致延迟,导致你的应用程序的迟缓的用户体验。为了避免这种行为,你必须使用静态地图的替代品。这将使地图延迟加载在你的列表视图和therby用户不会有现在再挖掘的地图。

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文件。然后,在我的适配器的活动,这种方法会导致在你的标记显示的坐标在运行时获取的应用程序映射的延迟加载,避免了在code繁重的工作。

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.

希望这将帮助!

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

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