Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 734
whenrybruce Avatar asked Dec 10 '12 20:12

whenrybruce


2 Answers

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

mMapView.onCreate(savedInstanceState);

Take a look at the API Reference

like image 195
Greeny Avatar answered Sep 28 '22 03:09

Greeny


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);        
like image 25
whenrybruce Avatar answered Sep 28 '22 02:09

whenrybruce