简体   繁体   中英

google map is not showing marker on it

this is my activity

public class FindPeopleFragment extends Fragment implements OnMapReadyCallback {
    private GoogleMap googleMap;
    MapFragment fragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
        initilizeMap();
        return rootView;
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void initilizeMap() {
        MapFragment fragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map1);
        if (fragment != null) {
            fragment.getMapAsync(this);

        }

    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        LatLng marker = new LatLng(44.797283, 20.460663);

        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(44.797283, 20.460663), 12));

        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(44.797283, 20.460663))
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
        googleMap.setMyLocationEnabled(true);

    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void onPause() {

        final FragmentManager fragManager = this.getFragmentManager();
        final Fragment fragment = fragManager.findFragmentById(R.id.map1);
        if (fragment != null) {
            fragManager.beginTransaction().remove(fragment).commit();
            super.onPause();

        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        final FragmentManager fragManager = this.getFragmentManager();
        final Fragment fragment = fragManager.findFragmentById(R.id.map1);
        if (fragment != null) {
            fragManager.beginTransaction().remove(fragment).commit();
        }
    }
}

I am using navigation drawer to show the map. my map is working but the marker on it does not show. I have used all permission and created the xml file well. I don't know what is the problem, I can't find a solution for this anywhere. I think I am having problem as I am using fragment, but I need it as I am using navigation drawer. please anybody? give me a solution, I'm stuck at this

You should implement onMapReadyCallback , you should set marker when map is ready. Once an instance of this interface is set on a MapFragment or MapView object, the onMapReady(GoogleMap) method is triggered when the map is ready to be used and provides a non null instance of GoogleMap.

The onMapReady() and method might not being called. Try to implement SupportMapFragment object that you retrieved from the XML layout. Then, call getMapAsync() .

supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(final GoogleMap googleMap) {
    // ...
}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM