Android的ListView中嵌入MapView,再从MapView中获取GoogleMap返回null

iman123 发布于 2014/06/25 20:54
阅读 1K+
收藏 0

华为云11月刊推送:DIY微信问答机器人,高性能计算代码的20个技巧!>>>

我知道MapView是一个相对重量级的控件,但我就是想做一个东西,ListView里每一行有个MapView(Google Maps Android API)显示一些地图,但从MapView中获取GoogleMap始终返回null,就导致不能显示地图。已排除API KEY的问题(我在其他地方一个Activity中显示一个MapView没有问题)
我上网查了一些资料,但还是没能解决
相关代码如下

public class TraceHistoryCursorAdapter extends CursorAdapter {
    private List<Marker> mMarkers = new ArrayList<Marker>();
    private List<Polyline> mPolyLines = new ArrayList<Polyline>();

    public TraceHistoryCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // when the view will be created for first time,
        // we need to tell the adapters, how each item will look
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater.inflate(R.layout.trace_history_list_item, parent, false);

        return retView;
    }

    @Override
    public void bindView(View view, Context context, final Cursor cursor) {
        // here we are setting our data
        // that means, take the data from the cursor and put it in views

        final MapView mapView = (MapView) view.findViewById(R.id.trace_history_list_item_map);
        TextView textViewDistance = (TextView) view.findViewById(R.id.trace_history_list_item_text_view_distance);
        TextView textViewTotalTime = (TextView) view.findViewById(R.id.trace_history_list_item_text_view_total_time);
        TextView textViewAverageSpeed = (TextView) view.findViewById(R.id.trace_history_list_item_text_view_average_speed);
        TextView textViewStartDatetime = (TextView) view.findViewById(R.id.trace_history_list_item_text_view_start_date_time);

        String distanceString = cursor.getString(cursor.getColumnIndex(DBHelper.TRACE_RECORD_DISTANCE));
        String totalTimeString = cursor.getString(cursor.getColumnIndex(DBHelper.TRACE_RECORD_TOTAL_TIME));

        textViewDistance.setText(distanceString);
        textViewTotalTime.setText(totalTimeString);
        textViewAverageSpeed.setText(Utils.formatFloatNumber(Double.parseDouble(distanceString) / Double.parseDouble(totalTimeString)));
        textViewStartDatetime.setText(cursor.getString(cursor.getColumnIndex(DBHelper.TRACE_RECORD_START_DATETIME)));

        cleanup();
        // http://stackoverflow.com/questions/14909917/in-google-maps-v2-fragment-getmap-returns-null
        // http://stackoverflow.com/questions/20689861/android-getmap-null-inside-dialogfragment
        // The map is pretty complex and is initializing a lot of state on and off the render thread.
        GoogleMap map = mapView.getMap();
        if (map != null) {
            // 绘制地图
            String latLngString = cursor.getString(cursor.getColumnIndex(DBHelper.TRACE_RECORD_LOCATIONS));
            List<Location> locations = Utils.stringToLocations(latLngString);
            Marker startMarker = map.addMarker(new MarkerOptions()
                    .position(Utils.locationToLatLng(locations.get(0)))
                    .title("起点")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
            Marker endMarker = map.addMarker(new MarkerOptions()
                    .position(Utils.locationToLatLng(locations.get(locations.size() - 1)))
                    .title("终点")
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)));
            mMarkers.add(startMarker);
            mMarkers.add(endMarker);
            for (int i = 0; i < locations.size() - 1; i++) {
                Polyline polyline = map.addPolyline(new PolylineOptions()
                        .add(Utils.locationToLatLng(locations.get(i)))
                        .add(Utils.locationToLatLng(locations.get(i + 1)))
                        .width(5)
                        .color(Color.BLUE)
                        .geodesic(true));
                mPolyLines.add(polyline);
            }
        }
//        mapView.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                GoogleMap map = mapView.getMap();
//                if (map != null) {
//                    // 绘制地图
//                    String latLngString = cursor.getString(cursor.getColumnIndex(DBHelper.TRACE_RECORD_LOCATIONS));
//                    List<Location> locations = Utils.stringToLocations(latLngString);
//                    Marker startMarker = map.addMarker(new MarkerOptions()
//                            .position(Utils.locationToLatLng(locations.get(0)))
//                            .title("起点")
//                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
//                    Marker endMarker = map.addMarker(new MarkerOptions()
//                            .position(Utils.locationToLatLng(locations.get(locations.size() - 1)))
//                            .title("终点")
//                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)));
//                    mMarkers.add(startMarker);
//                    mMarkers.add(endMarker);
//                    for (int i = 0; i < locations.size() - 1; i++) {
//                        Polyline polyline = map.addPolyline(new PolylineOptions()
//                                .add(Utils.locationToLatLng(locations.get(i)))
//                                .add(Utils.locationToLatLng(locations.get(i + 1)))
//                                .width(5)
//                                .color(Color.BLUE)
//                                .geodesic(true));
//                        mPolyLines.add(polyline);
//                    }
//                }
//            }
//        }, 3000);
    }

    private void cleanup() {
        for(Marker marker : mMarkers) {
            marker.remove();
        }
        for(Polyline polyline : mPolyLines) {
            polyline.remove();
        }
    }
}




以下是问题补充:

@iman123:最主要的问题就是这一行GoogleMap map = mapView.getMap();返回空,已经在Application的OnCreate中执行了MapsInitializer.initialize(this); (2014/06/25 20:57)
加载中
返回顶部
顶部