whenrybruce
whenrybruce

Reputation: 91

Android programmatically created MapView v2 is not displayed

I updated RawMapViewDemoActivity.java in the Android Google Maps v2 sample app to programmatically create a MapView but map is not displayed. I just get a blank screen.

I replaced

    mMapView = (MapView) findViewById(R.id.map);

with

    GoogleMapOptions options = new GoogleMapOptions();
    options.camera(new CameraPosition(new LatLng(0, 0), 15, 0, 0));         
    mMapView = new MapView(this, options);
    mMapView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));        

What am I doing wrong?

Upvotes: 6

Views: 5171

Answers (2)

whenrybruce
whenrybruce

Reputation: 91

Sorry - I fixed this a while ago, but forgot to post the answer.

It seems that a MapView must be placed in a layout container before it will be correctly displayed. The following snippet shows what I did to make the sample work.

LinearLayout linearLayout = new LinearLayout(this);
GoogleMapOptions options = new GoogleMapOptions();
options.camera(new CameraPosition(new LatLng(0, 0), 1, 0, 0));          
mMapView = new MapView(this, options);
linearLayout.addView(mMapView);
setContentView(linearLayout);        

Upvotes: 1

Greeny
Greeny

Reputation: 1951

Have you forwarded all the livecycle methods to the new MapView?

mMapView.onCreate(savedInstanceState);

Take a look at the API Reference

Upvotes: 2

Related Questions