GoogleMapView 未加载图块
GoogleMapView is not loading tiles
我有简单的 Fragment class,专门用于显示 Google 地图:
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。但是在屏幕上我看到了这张图片:
对象 Google地图真的很漂亮 - 例如,我可以打开“我的位置”按钮,然后我会在屏幕上看到它。那么为什么地图图块未加载???
你可以让它像这样工作。这还有一个额外的好处,即如果需要,片段还可以显示其他元素。
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 播放服务应该在设备中可用。你可以用这个检查。
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" />
您必须调用“getMapAsync()” 函数来显示 map.In 您的 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)
我有简单的 Fragment class,专门用于显示 Google 地图:
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。但是在屏幕上我看到了这张图片:
对象 Google地图真的很漂亮 - 例如,我可以打开“我的位置”按钮,然后我会在屏幕上看到它。那么为什么地图图块未加载???
你可以让它像这样工作。这还有一个额外的好处,即如果需要,片段还可以显示其他元素。
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 播放服务应该在设备中可用。你可以用这个检查。
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" />
您必须调用“getMapAsync()” 函数来显示 map.In 您的 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)