Maps fragment in tabbed activity is not showing the markers?

559 Views Asked by At

Using generic tabbed activity from the android studio and google maps. Map appears but without the markers. I have no idea how to fix this, hope it's understandable. Referencing the tab:

else if (getArguments().getInt(ARG_SECTION_NUMBER)==2){
            View rootView = inflater.inflate(R.layout.fragment_sub_page2, container, false);

            return rootView;
        }

Fragment tab

public class SubPage2 extends Fragment implements OnMapReadyCallback {
public GoogleMap mMap;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_sub_page2, container,false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    android.app.FragmentManager fragment = getActivity().getFragmentManager();
    final MapFragment myMapFragment = (MapFragment) fragment.findFragmentById(R.id.map);
    myMapFragment.getMapAsync(this);

}

@Override
public void onMapReady(GoogleMap googleMap) {
 mMap=googleMap;
      LatLng marker = new LatLng(-33.867, 151.206);

   mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker, 13));

    mMap.addMarker(new MarkerOptions().title("Hello Google Maps!").position(marker));
}

Subpage 2 layout

<?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"
 xmlns:tools="http://schemas.android.com/tools">

 <fragment android:id="@+id/map"
android:layout_width="wrap_content"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"

>

</fragment>

 </FrameLayout>

In case anyone else has the same problem the solution is adding a few lines in the calling statement - >

   else if (getArguments().getInt(ARG_SECTION_NUMBER)==2){
            View rootView = inflater.inflate(R.layout.fragment_sub_page2, container, false);
            FragmentManager manager = getActivity().getSupportFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.replace(R.id.map, new SubPage2()).commit();
            return rootView;
1

There are 1 best solutions below

6
Mapsy On

Your code looks okay. Just a hunch, have you checked to make sure you've tried a range of zoom levels to determine whether the text might be too tiny to see?

You can enable the zoom controls using mMap.getUiSettings().setZoomControlsEnabled(true);.

There's a possibility that the Title might only control the text that's displayed for that marker once it has gained focus, i.e. been clicked on. To eliminate any chance of uploading an empty marker, could you try supplying a BitmapDescriptor too?

BitmapDescriptor Example

// Allocate the MarkerBitmap.
final BitmapDescriptor lMarkerBitmap = BitmapDescriptorFactory.fromResource(R.drawable.my_marker_image);
// Add the Marker.
mMap.addMarker(new MarkerOptions().alpha(1.0f)
                                  .position(marker)
                                  .title("Hello Google Maps!")
                                  .icon(lMarkerBitmap));