Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapview displays blank but fragment is ok

I am trying to use Google Map V2. When I use map fragment .. Everything works. However if I use mapview instead, then it displays blank activity.

What could be wrong? Xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mapParent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
     <com.google.android.gms.maps.MapView 
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/>

     </LinearLayout>

Main activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
like image 744
Snake Avatar asked Dec 25 '22 02:12

Snake


1 Answers

You need to initialize the map yourself:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Gets the MapView from the XML layout and creates it
    MapView mapView = (MapView) findViewById(R.id.map);
    mapView.onCreate(savedInstanceState);

    // Gets to GoogleMap from the MapView and does initialization stuff
    GoogleMap map = mapView.getMap();

    // Needs to call MapsInitializer before doing any CameraUpdateFactory calls
    MapsInitializer.initialize(this);

    mapView.onResume();
}
like image 87
Simas Avatar answered Jan 06 '23 12:01

Simas