Creating MapView programmatically in Android MapsV2

Android: when creating a Map I get a blank page. I also have a valid API key and its installation in the manifest. my activity.

        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(" "));
        }
+4
source share
2 answers

I had the same problem because I was creating a MapView inside a fragment and forgot to add a method onResumeto the fragment code.

        mapView map = new MapView(getActivity(), options));
        map.setClickable(true);
        map.onCreate(savedInstanceState);
        map.onResume();

Hope this helps someone

+1
source

Add setContentView(relativeLayout);after this line relativeLayout.addView(mapView);This will solve your problem.

0
source

All Articles