1

i want to make maps in android fragment with use extend fragment

this is my code : Maps do not come up, and system force close

  @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.gmaps, container, false);

        SupportMapFragment mapFragment = ((SupportMapFragment) getActivity().getSupportFragmentManager() .findFragmentById(R.id.map));
        mapFragment.getMapAsync((OnMapReadyCallback) this.getActivity());

        btnFindPath = (Button) view.findViewById(R.id.btnFindPath);
        etOrigin = (EditText) view.findViewById(R.id.etOrigin);
        etDestination = (EditText) view.findViewById(R.id.etDestination);

        btnFindPath.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendRequest();
            }
        });

        return view;

please help me, and thank you

Däñish Shärmà
  • 2,891
  • 2
  • 25
  • 43

3 Answers3

1

You can try the below code :

 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.i("onActivityCreated", "onActivityCreated");
    if(supportMapFragment==null){
        supportMapFragment = (SupportMapFragment)getChildFragmentManager().findFragmentById(R.id.map);

        supportMapFragment.getMapAsync(new OnMapReadyCallback() {
           @Override
             public void onMapReady(GoogleMap googleMap) {
             mGoogleMap = googleMap;

            loadMapAtPosition(googleMap, CAMERA_INITIAL_LATITUDE, CAMERA_INITIAL_LONGITUDE);
        }
    });
 }

.....

private void loadMapAtPosition(GoogleMap googleMap, double latitude,    double longitude){     googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 6.0f)); }
Riten
  • 2,879
  • 3
  • 24
  • 28
1

Just Simple if you want to use Map in Fragment.

Try this

in you Fragment class.

 GoogleMap map = ((SupportMapFragment) getFragmentManager()
                .findFragmentById(R.id.map)).getMap();

and in your xml.

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" />
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
1

if you use google map within fragment then you should be use map object like this.you have to use getChildFragmentManager() to find fragment

    <fragment
        android:id="@+id/main_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" >
    </fragment>

     SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                    .findFragmentById(R.id.main_map);
Mahadev Dalavi
  • 173
  • 1
  • 7