androidgoogle-mapsfragmentandroid-tabbed-activity

Add Marker to Google Map in SupportFragment


I did a tabbed-activity with for tabs. In the fourth tab I did a map, and I added a marker but the marker is not visible and I do not undertand why. I tried many solutions for my problem, but for now I have come to this situation.

This is my Java code:

public class tab_mappa extends SupportMapFragment implements OnMapReadyCallback {

    private GoogleMap googleMap;
    SupportMapFragment mSupportMapFragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        super.onCreateView(inflater, container, savedInstanceState);
        View root = inflater.inflate(R.layout.tabmappa, null, false);
        mSupportMapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
        if (mSupportMapFragment == null) {
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            mSupportMapFragment = SupportMapFragment.newInstance();
            fragmentTransaction.replace(R.id.map, mSupportMapFragment).commit();
        }

        return root;
    }

    @Override
    public void onMapReady(final GoogleMap googleMap) {
        this.googleMap = googleMap;
        this.googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

        this.googleMap.getUiSettings().setZoomControlsEnabled(true);
        this.googleMap.getUiSettings().setCompassEnabled(true);
        this.googleMap.getUiSettings().setMyLocationButtonEnabled(true);
        this.googleMap.getUiSettings().setZoomGesturesEnabled(true);
        this.googleMap.getUiSettings().setRotateGesturesEnabled(true);

        LatLng pos1 = new LatLng(44.783878,10.879663);
        final LatLngBounds.Builder builder = new LatLngBounds.Builder();
        builder.include(pos1);

        this.googleMap.addMarker(new MarkerOptions().position(pos1));
        this.googleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(builder.build(), 100));
            }
        });

    }

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

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

    private void setUpMapIfNeeded() {
        if (googleMap == null) {
            getMapAsync(this);
        }
    }
    }

This is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical"
tools:context="esame.progetto.xhondar.github.com.info.tab_mappa">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.03"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map" />

</LinearLayout>

And this is my final result:

Do you have any ideas?


Solution

  • I solved the problem, this is the solution for those who need it:

    Java code:

    import android.support.v4.app.Fragment;
    public class tab_mappa extends Fragment implements OnMapReadyCallback {
            GoogleMap map;
            SupportMapFragment mapFragment;
    
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                View v = inflater.inflate(R.layout.tabmappa, container, false);
                SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager().findFragmentById(R.id.map);
                mapFragment.getMapAsync(this);
    
                return v;
            }
    
            @Override
            public void onMapReady(GoogleMap googleMap){
                map = googleMap;
                this.map = googleMap;
                this.map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    
                this.map.getUiSettings().setZoomControlsEnabled(true);
                this.map.getUiSettings().setCompassEnabled(true);
                this.map.getUiSettings().setMyLocationButtonEnabled(true);
                this.map.getUiSettings().setZoomGesturesEnabled(true);
                this.map.getUiSettings().setRotateGesturesEnabled(true);
    
                LatLng pp = new LatLng(44.783878,10.879663);
                map.addMarker(new MarkerOptions().position(pp).title("Carpi"));
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(pp, 8));
            }
        }
    

    Xml code:

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="esame.progetto.xhondar.github.com.info.tab"/>
    

    The error was located in xml code: is Fragment and not FrameLayout, this thing brought conflict within the code.