3

I have to set up maps fragment for PagerView in the main activity. This is the code of fragment.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.doctorfinderapp.doctorfinder.R;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class DoctorMapsFragment extends Fragment implements OnMapReadyCallback {

private GoogleMap googleMap;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.doctor_maps_fragment, container, false);
    SupportMapFragment mapFragment = SupportMapFragment.newInstance();
    getChildFragmentManager().beginTransaction().add(R.id.map,mapFragment);
    mapFragment.getMapAsync(this);
    return view;

}

@Override
public void onMapReady(GoogleMap gMap) {
    googleMap = gMap;
    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

@Override
public void onResume() {
    super.onResume();
}

@Override
public void onPause() {
    super.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}
}

this is doctor_maps_fragment.xml

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

how can i fix it? when i stay on map the layout display a white screen and i don't know what is the issue.

  • It looks like it would be better for you to just make your Fragment extend SupportMapFragment, take a look at the TabLayout solution in this answer: http://stackoverflow.com/a/32579020/4409409 – Daniel Nugent Feb 11 '16 at 00:12
  • Thanks but it was not my same problem. – Francesco Starna Feb 11 '16 at 00:51
  • I don't think the root element can be a fragment tag. Why not give it a framelayout and use SupportMapFragment.newInstance() and then use it to replace the framelayout. https://developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment.html#newInstance%28%29 – Ophitect Feb 11 '16 at 14:02
  • There is now another exception – Francesco Starna Feb 11 '16 at 15:29

2 Answers2

0

Multiple things, your layout is wrong, since you're using 'MapView' there is no 'android:name' attribute.

If you wanna use MapView and not SupprtMapFragment change your layout to:

<com.google.android.gms.maps.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Also you must call the mapViews onCreate function, otherwise nothing will be shown, so change your onCreateView to:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.doctor_maps_fragment, container, false);
    mMapView = (MapView) view.findViewById(R.id.map);
    mMapView.onCreate(null);
    mMapView.getMapAsync(this);
    return view;
}
Andre Classen
  • 3,868
  • 3
  • 26
  • 36
0
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.doctor_maps_fragment, container, false);
    SupportMapFragment mMapFragment = SupportMapFragment.newInstance();
    getChildFragmentManager().beginTransaction().add(R.id.map, mMapFragment).commit();
    mMapFragment.getMapAsync(this);
    return view;

}

xml file

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/map"/>
Ophitect
  • 543
  • 4
  • 18