谷歌地图没有显示标记

时间:2016-05-16 04:19:50

标签: android google-maps google-maps-markers android-navigation-drawer

这是我的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();
        }
    }
}

我正在使用导航抽屉来显示地图。我的地图正在运行,但它上面的标记没有显示。我已经使用了所有权限并且很好地创建了xml文件。我不知道是什么问题,我无法在任何地方找到解决方案。我觉得我有问题,因为我正在使用片段,但我需要它,因为我使用导航抽屉。请问任何人?给我一个解决方案,我坚持这个

1 个答案:

答案 0 :(得分:0)

您应该实施onMapReadyCallback,您应该在地图准备好后设置标记。在MapFragmentMapView对象上设置此接口的实例后,当地图准备好使用时会触发onMapReady(GoogleMap)方法并提供GoogleMap的非空实例。< / p>

可能未调用onMapReady()方法。尝试实现从XML布局检索的SupportMapFragment对象。然后,拨打getMapAsync()

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