在Android MapsV2中以编程方式创建MapView

在Android MapsV2中以编程方式创建MapView,android,google-maps-api-3,android-maps-v2,Android,Google Maps Api 3,Android Maps V2,Android:在创建地图时 我得到了空白页。我有一个有效的API密钥,并将其设置为清单。 我的活动 relativeLayout = new RelativeLayout(context); RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_P

Android:在创建地图时 我得到了空白页。我有一个有效的API密钥,并将其设置为清单。 我的活动

        relativeLayout = new RelativeLayout(context);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        relativeLayout.setLayoutParams(layoutParams);
        GoogleMapOptions googleMapOptions = new GoogleMapOptions();
        googleMapOptions.mapType(GoogleMap.MAP_TYPE_NORMAL)
                .compassEnabled(false).rotateGesturesEnabled(false)
                .tiltGesturesEnabled(false);

        googleMapOptions.camera(new CameraPosition(new LatLng(0, 0), 3, 0,
                0));
        mapView = new MapView(context, googleMapOptions);


        mapView.onCreate(new Bundle());
        relativeLayout.addView(mapView);

        Double[][] latlang = mapData.getLatlang();

        marker = new Marker[mapData.getLatlang().length];
        for (int i = 0; i < mapData.getLatlang().length; i++) {
            this.marker[i] = mapView.getMap()
                    .addMarker(
                            new MarkerOptions()
                                    .position(
                                            new LatLng(latlang[i][0],
                                                    latlang[i][1]))
                                    .title(" ")
                                    .snippet(" "));
        }
relativeLayout=新的relativeLayout(上下文);
RelativeLayout.LayoutParams LayoutParams=新的RelativeLayout.LayoutParams(
LayoutParams.FILL\u父级,LayoutParams.FILL\u父级);
relativeLayout.setLayoutParams(layoutParams);
GoogleMapOptions GoogleMapOptions=新的GoogleMapOptions();
googleMapOptions.mapType(GoogleMap.MAP\u TYPE\u NORMAL)
.compassEnabled(假)。rotategesturresenabled(假)
.tiltGesturesEnabled(假);
googleMapOptions.摄像头(新摄像头位置)(新车床(0,0),3,0,
0));
mapView=新地图视图(上下文,谷歌地图选项);
onCreate(newbundle());
relativeLayout.addView(mapView);
Double[]latlang=mapData.getLatlang();
marker=新标记[mapData.getLatlang().length];
for(int i=0;i
添加
设置内容视图(relativeLayout)在这一行之后
relativeLayout.addView(mapView)它会解决您的问题。

我也遇到了同样的问题,因为我在片段中创建MapView时忘记了将
onResume
方法添加到片段代码中

        mapView map = new MapView(getActivity(), options));
        map.setClickable(true);
        map.onCreate(savedInstanceState);
        map.onResume();
希望这对别人有帮助