我将尝试在我的Android应用程序中在名为RoeteFragment
的片段上显示地图。如果我调试代码,我看到永远不会调用方法 onMapReady
,因此不会加载映射。
该片段根据需要实现OnMapReadyCallback
,在onCreateView
中,我得到了MapView
并调用getMapAsync
.如果我在该方法上放置一个断点,它将始终被命中,onMapReady
里面的断点永远不会被命中。没有引发异常。
我在文件res/values/google_maps_api.xml
中还有一个有效的谷歌地图API密钥。
这是我的代码:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
</RelativeLayout>
public class RoeteFragment extends Fragment implements OnMapReadyCallback {
private MapView mapView;
private static Roete _roete;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_roete, container, false);
mapView = (MapView) view.findViewById(R.id.map);
mapView.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
if (_roete != null && _roete.getAankomstLocatie() != null && _roete.getVertrekLocatie() != null) {
LatLng aankomst = new LatLng(_roete.getAankomstLocatie().getLatitude(), _roete.getAankomstLocatie().getLongitude());
googleMap.addMarker(new MarkerOptions().position(aankomst).title("aankomst"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(aankomst));
LatLng vertrek = new LatLng(_roete.getVertrekLocatie().getLatitude(), _roete.getVertrekLocatie().getLongitude());
googleMap.addMarker(new MarkerOptions().position(vertrek).title("vertrek"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(vertrek));
}
}
public static Fragment newInstance() {
return new RoeteFragment ();
}
public static Fragment newInstance(Roete roete) {
_roete = roete;
return newInstance();
}
}
你能在我的代码中提交错误吗?
阅读MapView
文档。特别是:
此类的用户必须将所有生命周期方法从包含此视图的
Activity
或Fragment
转发到此类中的相应方法。特别是,您必须转发以下方法:
onCreate(Bundle)
onResume()
onPause()
onDestroy()
onSaveInstanceState()
onLowMemory()
地图片段是您膨胀为另一个片段的布局的一部分(我们称之为父级)。膨胀的片段不是活动的子片段,而是父片段的子片段。您必须询问父片段的子片段管理器:
注意:只需扩展简单片段public class MapsFragments extends Fragment implements OnMapReadyCallback {}
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="net.elektrasoft.Test.MapsFragments" />
和
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("check","onCreateView");
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_maps_fragments, container, false);
// Gets the MapView from the XML layout and creates it
final SupportMapFragment myMAPF = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
myMAPF.getMapAsync(this);
return view;
}`
尝试使用支持地图片段。它也将在碎片中工作。https://developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment#developer-guide
对于相机和变焦级别,请检查此 -https://developers.google.com/maps/documentation/android-api/views#introduction
试试这个,如果它有助于接受它作为答案。
您应该从文档中注意到这一点:
在完全交互模式下使用 API 时,
MapView
类的用户必须将所有活动生命周期方法转发给MapView
类中的相应方法。 生命周期方法的例子包括onCreate()
、onDestroy()
、onResume()
和onPause()
。在精简模式下使用
MapView
类时,转发生命周期事件是可选的,但以下情况除外:
必须调用
onCreate()
,否则不会出现地图。如果您希望在精简模式地图上显示我的位置点并使用默认位置源,则需要调用
onResume()
和onPause()
,因为位置源只会在这些调用之间更新。如果使用自己的位置源,则无需调用这两个方法。
所以,你在onCreateView
中缺少mapView.onCreate(mapViewBundle)
,并且由于您的地图处于完全交互模式,因此您必须照顾整个生命周期,如下所示:
public class RoeteFragment extends Fragment implements OnMapReadyCallback {
private static final String MAPVIEW_BUNDLE_KEY = "MapViewBundleKey";
private MapView mapView;
private static Roete _roete;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_roete, container, false);
mapView = (MapView) view.findViewById(R.id.map);
// *** IMPORTANT ***
// MapView requires that the Bundle you pass contain ONLY MapView SDK
// objects or sub-Bundles.
Bundle mapViewBundle = null;
if (savedInstanceState != null) {
mapViewBundle = savedInstanceState.getBundle(MAPVIEW_BUNDLE_KEY);
}
mapView.onCreate(mapViewBundle);
mapView.getMapAsync(this);
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
if (_roete != null && _roete.getAankomstLocatie() != null && _roete.getVertrekLocatie() != null) {
LatLng aankomst = new LatLng(_roete.getAankomstLocatie().getLatitude(), _roete.getAankomstLocatie().getLongitude());
googleMap.addMarker(new MarkerOptions().position(aankomst).title("aankomst"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(aankomst));
LatLng vertrek = new LatLng(_roete.getVertrekLocatie().getLatitude(), _roete.getVertrekLocatie().getLongitude());
googleMap.addMarker(new MarkerOptions().position(vertrek).title("vertrek"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(vertrek));
}
}
public static Fragment newInstance() {
return new RoeteFragment ();
}
public static Fragment newInstance(Roete roete) {
_roete = roete;
return newInstance();
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Bundle mapViewBundle = outState.getBundle(MAPVIEW_BUNDLE_KEY);
if (mapViewBundle == null) {
mapViewBundle = new Bundle();
outState.putBundle(MAPVIEW_BUNDLE_KEY, mapViewBundle);
}
mapView.onSaveInstanceState(mapViewBundle);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}
根据@androidnoobdev的评论,他说要使用SupportMapFragment
,我将布局文件中的代码更新为:
<?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
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</RelativeLayout>
更新删除了支持片段后不需要的 MapView b'z。
感谢@androidnoobdev
先让你知道这一点。我正在使用谷歌地图片段作为另一个片段中的子片段,其中GoogleMapFragment扩展com.google.android.gms.maps.SupportMapFragment
我在onCreateView的GoogleMapFragment中使用了这段代码,你也可以在onStart中使用。当 onMapReady 未调用时
MapFragment mapFragment = new MapFragment();
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
mCallback.onMapReady(map);
}
});
我刚刚删除了这个
MapFragment mapFragment = new MapFragment();
然后开始像这样打电话
this.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
Log.i(TAG, "onMapReady: onCreateView");
mCallback.onMapReady(map);
}
});
我希望这可能会对某人有所帮助。