Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 谷歌地图未显示在选项卡片段中_Android_Google Maps - Fatal编程技术网

Android 谷歌地图未显示在选项卡片段中

Android 谷歌地图未显示在选项卡片段中,android,google-maps,Android,Google Maps,我正在项目中使用选项卡布局。其中一个选项卡片段应该显示世界地图。我的应用程序与谷歌地图API集成,我使用ViewPager设置适配器。下面是活动 public class HomeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(

我正在项目中使用选项卡布局。其中一个选项卡片段应该显示世界地图。我的应用程序与谷歌地图API集成,我使用ViewPager设置适配器。下面是活动

public class HomeActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    PagerAdapter pagerAdapter =
            new PagerAdapter(getSupportFragmentManager(), HomeActivity.this);
    viewPager.setAdapter(pagerAdapter);

    // Give the TabLayout the ViewPager
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.setupWithViewPager(viewPager);

    // Iterate over all tabs and set the custom view
    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        tab.setCustomView(pagerAdapter.getTabView(i));
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

class PagerAdapter extends FragmentPagerAdapter {

    String tabTitles[] = new String[] { "Cloud", "Home", "Report" };
    Context context;

    public PagerAdapter(FragmentManager fm, HomeActivity context) {
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return tabTitles.length;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new MapFragment();
            case 1:
                return new OneFragment();
            case 2:
                return new TwoFragment();
        }

        return null;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }

    public View getTabView(int position) {
        View tab = LayoutInflater.from(HomeActivity.this).inflate(R.layout.custom_tab, null);
        TextView tv = (TextView) tab.findViewById(R.id.custom_text);
        tv.setText(tabTitles[position]);
        return tab;
    }

}

    }
我的应用程序正在成功运行。在第一个选项卡中,可以加载片段,当单击屏幕时,我也可以看到纬度和经度值。但是看不到谷歌地图。在图片下方共享。相同的代码在不同的活动中完美地工作。请帮我找到我没有补充的内容。

我已经解决了这个问题,为Google map API生成了一个新的密钥,并在清单文件中进行了更新。现在,我可以在我的一个标签片段中看到谷歌地图与其他片段一起完美工作。

添加到touch listener以查看您显示大陆的位置,并根据您显示大陆的方式,确定接触点是否位于该大陆或该大陆的形状内。任何帮助请参阅我的评论中的帮助,不是吗?@Vladyslav Matviienko,我已经用完整的代码详细编辑了这个问题。我想知道为什么我不能在我的标签片段中看到地图。你编辑它的方式与原来的问题完全不同。你不可能通过这种方式得到答案,因为你的问题已经离第一页很远了,没有人会再看到它了。恢复原来的问题,改为问新问题。
public class MapFragment extends Fragment  {

private MapView mMapView;

private GoogleMap mMap;

View view;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_map, null, false);
    mMapView = (MapView) view.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);
    mMapView.onResume();
    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    mMapView.getMapAsync(new OnMapReadyCallback() {

        @Override
        public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;

            // Add a marker in Sydney and move the camera
            LatLng sydney = new LatLng(-34, 151);
            mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
            mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
            mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                @Override
                public void onMapClick(LatLng latLng) {

                    Toast.makeText(
                            getActivity(),
                            "Lat : " + latLng.latitude + " , "
                                    + "Long : " + latLng.longitude,
                            Toast.LENGTH_SHORT).show();

                }
             });

            if (mMap == null) {
                mMapView = (MapView) view.findViewById(R.id.mapView);
                mMapView.onCreate(getArguments());
                mMapView.onResume();

                try {
                    MapsInitializer.initialize(getActivity());
                    mMapView.getMapAsync(new OnMapReadyCallback() {
                        @Override
                        public void onMapReady(GoogleMap gmap) {
                            mMap = gmap;
                            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

                            //fill markers
                            //add marker listener

                        }
                    });



                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

         }

    });
    return view;
}

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

    @Override
    public void onLowMemory(){
        super.onLowMemory();
        mMapView.onLowMemory();
    }


}