-->

Google Maps API v2 Custom MapFragment + SimpleFrag

2019-08-11 14:59发布

问题:

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 application must not use support v4 (sdk 11+) / FragmentActivity.

Empty fragment.

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black" >
</RelativeLayout>

Class:

public class FirstFragment extends Fragment{

public FirstFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.first_fragment, container, false);
    return view;
}
}

Map fragment (try to make it singleton).

XML:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.MapFragment"/>

Class

public class ThirdMapFragment extends MapFragment {
public static MapFragment instance;

@Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
    View view;
    view = arg0.inflate(R.layout.map_fragment, arg1, false);
    return view;
}

public static MapFragment getInstance() {
    if (instance == null) {
        instance = newInstance();
    }
    // NullPointerException in getMap() ???
    // instance.getMap().addMarker(new MarkerOptions().position(new LatLng(41.1, 39)));
    return instance;
}

@Override
public void onCreate(Bundle arg0) {
    super.onCreate(arg0);
}
}

Main activity:

public class ActivityMain extends Activity implements OnClickListener {
FragmentManager fragmentManager;
FragmentTransaction transaction;
FirstFragment firstFragment;
MapFragment mapFragment;

// Buttons
Button b4;

int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    fragmentManager = getFragmentManager();
    transaction = fragmentManager.beginTransaction();

    b4 = (Button) findViewById(R.id.button4);

    b1.setOnClickListener(this);

    firstFragment = new FirstFragment();
    mapFragment =  ThirdMapFragment.getInstance();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onClick(View v) {
    makeTransaction(v.getId());
}



private void makeTransaction(int id) {
    i++;
    transaction = fragmentManager.beginTransaction();
    if (i == 1) {
        transaction.replace(R.id.relativeLayout, firstFragment).commit();
    } else if (i == 2) {
        transaction.replace(R.id.relativeLayout, secondFragment).commit();
    } else {
    transaction.replace(R.id.relativeLayout, mapFragment).commit();
        i = 0;
    }
}
}

R.id.relativeLayout - Layout in main.xml

But I do not see the map (empty screen with zoom buttons) and how to make LocationListener? Thx.

回答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.*;

public class Map extends android.support.v4.app.FragmentActivity
{
private GoogleMap map;


@Override
protected void onCreate(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.



回答2:

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



回答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