Manivannan
Manivannan

Reputation: 1640

Create MapView Programmatically in Android MapsV2

Android: While creating Map I am getting the Blank page.I have a valid API key also and set it in 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(" "));
        }

Upvotes: 4

Views: 2253

Answers (2)

Alexey Indeev
Alexey Indeev

Reputation: 66

I had the same issue, because I was creating the MapView inside a Fragment and forgot to add the onResume method to the fragment code.

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

Hope this helps someone

Upvotes: 4

VVB
VVB

Reputation: 7641

Add setContentView(relativeLayout); after this line relativeLayout.addView(mapView); It'll solve your problem.

Upvotes: 0

Related Questions