0

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?

KKKKK
  • 273
  • 2
  • 18

2 Answers2

3

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.

KKKKK
  • 273
  • 2
  • 18
0

How about use marker without the builder? you wrote the code in onMapReady()

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));
     }
});

it change to

LatLng pos1 = new LatLng(44.783878,10.879663);

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

this one. I removed builder and this before googleMap.

SunSpike
  • 127
  • 1
  • 1
  • 12
  • 1
    @KKKKK lol I forgot about that. So how about just remove this before googleMap? this.googleMap means googleMap in your instance, but just googleMap means map in your method. – SunSpike Jun 18 '18 at 00:47
  • @KKKKK Well.. then How to use log Like Log("a", "a"); in your onMapReady? use log 'a', and 'b', you can search which code run first. Use log in if and check this code run or use both onMapReady and onCreateView. – SunSpike Jun 18 '18 at 00:55
  • Mean the LogCat? I don't know hot to use it :-( – KKKKK Jun 18 '18 at 01:04
  • @KKKKK Just use Log.e("Hello", "Bye"); in your Code. Then you can understand how to use LogCat. You can confirm the log in Run tab. not LogCat tab. – SunSpike Jun 18 '18 at 01:10
  • onCreateView run first – KKKKK Jun 18 '18 at 01:16
  • @KKKKK To resolve, you should many times debugging... Write the log in if, or make the breakpoint and run debug mode and check exists null value. – SunSpike Jun 18 '18 at 01:23
  • ___________ok ;-) – KKKKK Jun 18 '18 at 01:34