我與片段地圖評論,首先你應該refrenece你的觀點,你會叫的東西從它,這就是爲什麼我吹我的看法首先getActivity().getSupportFragmentManager()
View view = inflater.inflate(R.layout.activity_maps, null, false);
二呼兒片段經理this.getChildFragmentManager()
代替
這裏是完整的例子,以查看地圖
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class ClinicFragment extends Fragment implements OnMapReadyCallback {
private GoogleMap mMap;
public static ClinicFragment newInstance() {
ClinicFragment fragment = new ClinicFragment();
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_maps, null, false);
SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
return view;
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@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));
}
}
需要許可
<permission
android:name="your.package.name.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="your.package.name.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
加上特徵元素
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
大多數進口谷歌地圖鍵
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your_key_here" />
更新,如果你面對Error inflating class fragment
添加該元數據
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="your_key_here" />
夏日爲mainfest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name" >
// permission here
<application
android:name=".Activities.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
// feature element
// maps key
</application>
</manifest>
activity_maps.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.imaadv.leaynik.ClinicFragment" />
凡將是一個合適的地方調用'getMapAsync()'呢?基本上我的應用程序有3個片段,1個活動和一個ViewPager在這些片段之間滑動。應該是活動的'onCreate()'? – Neutrino
@Neutrino:「基本上我的應用程序有3個片段,1個活動和一個在這些片段之間滑動的ViewPager」 - 你不需要嵌套片段。你的活動有一個'ViewPager'。你的'PagerAdapter'創建三個片段。不需要嵌套。讓'FragmentPagerAdapter'直接創建'SupportMapFragment'的新實例;你不需要膨脹佈局。這個示例顯示了一個包含10個地圖片斷的'ViewPager':https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Pager – CommonsWare
對不起,我有點迷路。我的ViewPagerAdapter的getItem()在這種情況下檢查位置並調用'return new Example()' - 我猜在這種情況下需要膨脹佈局嗎?然後我不太確定我應該在哪裏做我的操作:在片段代碼,活動代碼或其他任何地方。 – Neutrino