How to implement Google Maps within a Fragment and use the GoogleMap object?

回眸只為那壹抹淺笑 提交于 2019-12-09 13:24:51

问题


How to implement GoogleMap in a Fragment?

I'm trying to develop the same thing, but getMap() cannot be executed because SupportMapFragment (when executing the line (SupportMapFragment)getActivity().getSupportFragmentManager().findFragmentById(‌​R.id.map) ) returns a null object.

I've tried extending MapFragment and SupportMapFragment instead of Fragment, but they didn't work...

I just don't want to create a new activity to show a map: I want to show a mere Map instead my main activity. Can I carry out my goal?

I have a main activity which extends ActionBarActivity, who replaces fragments inside a container (depending on the user selection on the drawer, X or Y fragment is replaced and shown). I own 2 fragments working, but I cannot accomplish the goal of manipulating a GoogleMap object inside a Fragment.

That's my mapa.xml

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

That's my FragmentMapa class:

  import com.google.android.gms.maps.GoogleMap;
   import com.google.android.gms.maps.SupportMapFragment;
   import com.ingartek.bizkaimove.R;

   import android.app.Activity;
   import android.os.Bundle;
   import android.support.v4.app.Fragment;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;

 public class FragmentMapa extends Fragment {

private GoogleMap mMapa;

public FragmentMapa() {
    // TODO Auto-generated constructor stub
}

@Override
public void onAttach (Activity activity) {
    super.onAttach(activity);
}

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

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.mapa, container, false);

    mMapa = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapaPrincipal)).getMap();      

    return rootView;

}

The line highlighted is not working, because the SupportMapFragment object is null and therefore, getMap() crashes.

Any help please? Thanks in advance.

SOLUTION TO MY PROBLEM

For anyone who faces a problem like mine: you shouldn't use getSupportFragmentManager(), instead you should use getChildFragmentManager(). This should remain that way:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.mapa, container, false);

    setupMapIfNeeded();

    return rootView;

}

private void setupMapIfNeeded() {

    if( mMapa == null ){
        SupportMapFragment smf = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapaPrincipal);

        if( smf != null ){
            Toast.makeText(getActivity(), "Let's go!!", Toast.LENGTH_SHORT).show();
            mMapa = smf.getMap();
        }else{
            Toast.makeText(getActivity(), "SMF is null...", Toast.LENGTH_SHORT).show();
        }

        if( mMapa != null ){

            setupMap();

        }

    }

}

private void setupMap() {
    // TODO Auto-generated method stub
    Toast.makeText(getActivity(), "Configure the map", Toast.LENGTH_SHORT).show();
}

回答1:


Hi you can refer my answer here

You need to call like this...

SupportMapFragment  fragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapaPrincipal);

mMapa = fragment.getMap();


来源:https://stackoverflow.com/questions/26863507/how-to-implement-google-maps-within-a-fragment-and-use-the-googlemap-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!