如何使用 ViewPager 将 Google Maps V2 放在 Fragment 上 [英] How to put Google Maps V2 on a Fragment using ViewPager

查看:137
本文介绍了如何使用 ViewPager 将 Google Maps V2 放在 Fragment 上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Play 商店中进行相同的选项卡布局.我必须使用片段显示 标签布局和来自 androidhive 的 viewpager. 但是,我无法实现 谷歌地图 v2 就可以了.我已经在互联网上搜索了几个小时,但找不到有关如何操作的教程.有人可以告诉我怎么做吗?

I am trying to do a tab layout same in Play Store. I got to display the tab layout using a fragments and viewpager from androidhive. However, I can't implement google maps v2 on it. I searched the internet for hours already, but I can't find a tutorial on how to do it. Can some one please show me how?

推荐答案

通过使用此代码,我们可以在任何地方设置 MapView,在任何 ViewPager、Fragment 或 Activity 中.

By using this code we can setup MapView anywhere, inside any ViewPager or Fragment or Activity.

Google for Maps 的最新更新中,片段仅支持 MapView.MapFragment &SupportMapFragment 对我不起作用.

在文件location_fragment.xml中设置显示地图的布局:

Setting up the layout for showing the map in the file location_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

现在,我们在文件 MapViewFragment.java 中设置用于显示地图的 Java 类:

Now, we setup the Java class for showing the map in the file MapViewFragment.java:

public class MapViewFragment extends Fragment {

    MapView mMapView;
    private GoogleMap googleMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.location_fragment, container, false);

        mMapView = (MapView) rootView.findViewById(R.id.mapView);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume(); // needed to get the map to display immediately

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                googleMap = mMap;

                // For showing a move to my location button
                googleMap.setMyLocationEnabled(true);

                // For dropping a marker at a point on the Map
                LatLng sydney = new LatLng(-34, 151);
                googleMap.addMarker(new MarkerOptions().position(sydney).title("Marker Title").snippet("Marker Description"));
            
                // For zooming automatically to the location of the marker
                CameraPosition cameraPosition = new CameraPosition.Builder().target(sydney).zoom(12).build();
                googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
            }
        });

        return rootView;
    }

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

最后,您需要通过在 Google Cloud Console<注册您的应用来获取应用的 API 密钥/a>.将您的应用注册为原生 Android 应用.

这篇关于如何使用 ViewPager 将 Google Maps V2 放在 Fragment 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆