如何把谷歌地图V2上的片段使用ViewPager [英] How to put Google Maps V2 on a Fragment Using ViewPager

查看:148
本文介绍了如何把谷歌地图V2上的片段使用ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个标签布局Play商店一样。我得到了显示使用碎片和viewpager从androidhive的标签布局。 的但是,我不能执行<一个href="http://wptrafficanalyzer.in/blog/driving-distance-and-travel-time-duration-between-two-locations-in-google-map-android-api-v2/">google地图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. Thanks!

推荐答案

通过使用这种code,我们可以设置MapFragment任何地方,任何内部或ViewPager片段或活动。

在这里,我使用的支持库以工作( SupportMapFragment 而不是 MapFragment ),从而使 MapFragment 可以的即使工作在下面的Andr​​oid 3.0。您可以替换 SupportMapFragment MapFragment 如果您正在使用的Andr​​oid 3.0或以上。

Here I'm using the Support Library to work (SupportMapFragment instead of MapFragment), so that the MapFragment can even work on below Android 3.0. You can replace the SupportMapFragment with the MapFragment if you are working on Android 3.0 or above.

首先,我们需要设置MainActivity这样我们就可以称之为 SupportFragmentManager 从里面的 LocationFragment 的.class显示地图

Firstly we need to setup the MainActivity so that we can call the SupportFragmentManager from inside the LocationFragment.class to show the Map:

public class MainActivity extends FragmentActivity {

public static FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // initialising the object of the FragmentManager. Here I'm passing getSupportFragmentManager(). You can pass getFragmentManager() if you are coding for Android 3.0 or above.
    fragmentManager = getSupportFragmentManager();
}
}

现在我们设计了显示地图的布局。 location_fragment.xml

Now we design the layout for the Showing the Map. 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" >

    <fragment
        android:id="@+id/location_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

现在我们$ C c中的java类$使用该SupportMapFragment 的:

public class LocationFragment extends Fragment {

private static View view;
/**
 * Note that this may be null if the Google Play services APK is not
 * available.
 */

private static GoogleMap mMap;
private static Double latitude, longitude;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    if (container == null) {
        return null;
    }
    view = (RelativeLayout) inflater.inflate(R.layout.location_fragment, container, false);
    // Passing harcoded values for latitude & longitude. Please change as per your need. This is just used to drop a Marker on the Map
            latitude = 26.78;
            longitude = 72.56;

            setUpMapIfNeeded(); // For setting up the MapFragment

    return view;
}

/***** Sets up the map if it is possible to do so *****/
public static void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) MainActivity.fragmentManager
                .findFragmentById(R.id.location_map)).getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null)
            setUpMap();
    }
}

/**
 * This is where we can add markers or lines, add listeners or move the
 * camera.
 * <p>
 * This should only be called once and when we are sure that {@link #mMap}
 * is not null.
 */
private static void setUpMap() {
    // For showing a move to my loction button
    mMap.setMyLocationEnabled(true);
    // For dropping a marker at a point on the Map
    mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("My Home").snippet("Home Address"));
    // For zooming automatically to the Dropped PIN Location
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,
            longitude), 12.0f));
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    if (mMap != null)
        setUpMap();

    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) MainActivity.fragmentManager
                .findFragmentById(R.id.location_map)).getMap(); // getMap is deprecated
        // Check if we were successful in obtaining the map.
        if (mMap != null)
            setUpMap();
    }
}

/**** The mapfragment's id must be removed from the FragmentManager
 **** or else if the same it is passed on the next time then 
 **** app will crash ****/
@Override
public void onDestroyView() {
    super.onDestroyView();
    if (mMap != null) {
        MainActivity.fragmentManager.beginTransaction()
            .remove(MainActivity.fragmentManager.findFragmentById(R.id.location_map)).commit();
        mMap = null;
    }
}
}

最后,我们需要设置的Manifest.xml这样我们就可以从内致电 SupportFragmentManager LocationFragment 的.class显示地图的。您可以通过注册在谷歌云控制台您的应用程序获得API密钥为您的应用程序。注册您的应用程序作为原生Android应用程序:

Finally we need to setup the Manifest.xml so that we can call the SupportFragmentManager from inside the LocationFragment.class to show the Map. You can get the API Key for your app by registering your app at Google Cloud Console. Register your app as Native Android App :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.arshad.map"
android:installLocation="auto"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- Creating Permission to receive Google Maps -->
<permission
    android:name="com.arshad.map.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<!-- Permission to receive Google Maps -->
<uses-permission android:name="com.arshad.map.permission.MAPS_RECEIVE" />

<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/app_icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <!-- Google Maps Fragment API Key Data -->
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value=<REPLACE WITH YOUR API KEY. GET IT FROM GOOGLE CLOUD CONSOLE> />

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <activity
        android:name="MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这篇关于如何把谷歌地图V2上的片段使用ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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