我有简单的Fragment类,专门用于显示Google Map:
public class MapFragment extends Fragment implements Map, OnMapReadyCallback {
private static final String TAG = MapFragment.class.getSimpleName();
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
MapView googleMapView = (MapView) view.findViewById(R.id.mapFragment_mapView);
googleMapView.onCreate(savedInstanceState);
googleMapView.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
Log.d(TAG, "onMapReady have called");
MapsInitializer.initialize(this.getActivity());
}
}
<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mapFragment_mapView" />
</FrameLayout>
当我运行我的应用程序时,我可以看到日志消息,这意味着我有一个有效的对象GoogleMap。但在屏幕上我看到这张图片:
对象GoogleMap真的很好 - 例如我可以打开“我的位置”按钮,我会在屏幕上看到它。那么为什么地图瓷砖没有加载???
答案 0 :(得分:1)
你可以让它像这样工作。这具有额外的好处,即如果需要,片段还可以具有其他元素。
public class DashBoardFragment extends Fragment{
private MapView mMapView;
private GoogleMap mGoogleMap;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_dash_board, container, false);
mMapView = (MapView) view.findViewById(R.id.map_dashBoard);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mGoogleMap = mMapView.getMap();
return view;
}
}
片段的xml就像这样
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.screens.DashBoardFragment">
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_dashBoard"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
Google Play服务应该在设备中提供。您可以使用此功能进行检查。
public static boolean isGooglePlayServiceAvailable(Activity context){
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
if(status==ConnectionResult.SUCCESS){
return true;
}else{
GooglePlayServicesUtil.getErrorDialog(status, context, 10).show();
return false;
}
}
最后但非常重要,我们的地图密钥应出现在清单中
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="your google map key" />
答案 1 :(得分:0)
您必须调用' getMapAsync()'函数来显示地图。在onCreateView中使用 getMapAsync()来设置片段的回调。
以下是getMapAsync的示例代码:
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map); mapFragment.getMapAsync(this);
您也可以尝试阅读MapFragment的官方Google文档: https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment#getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)