Skip to content Skip to sidebar Skip to footer

Google Maps Api V2 Custom Mapfragment + Simplefragment

I need to create a simple application which contain 1. Custom MapFragment which can display current location (LocationListener?), two markers, etc. 2. Simple empty Fragments. The

Solution 1:

if you are using the support library you have to use the SupportMapFragment class and not the MapFragment for the map fragment. your activity should be a FragmentActivity for the support of the library. as well as for the Fragment manager. you have to getSupportFragmentManager().

Take a look at this code:

import com.google.android.gms.common.GooglePlayServicesUtil;
import android.os.Bundle;
import com.google.android.gms.maps.*;

publicclassMapextendsandroid.support.v4.app.FragmentActivity
{
private GoogleMap map;


@OverrideprotectedvoidonCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()) == 0)
        {
            map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        }
}}

If you still don't see a map after you made those changes. Check your API Console that you have turn on the Google Map ANDROID API V2 and not just the Google Map API V2 that could cause the same result as you describe.

Solution 2:

the view is not created yet that's why getMap() returns null, map doesn't exist, put your code in onResume

Solution 3:

Put in the manifest, the key for android? For example:

<application
        android:icon="@drawable/android"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data android:name="com.google.android.maps.v2.API_KEY"
            android:value="AIzaSyB2s6isdfsghDfgdfdfgDFGDG-yi4-aAYUma0Owc"/>

https://developers.google.com/maps/documentation/android/start

Post a Comment for "Google Maps Api V2 Custom Mapfragment + Simplefragment"