我正在使用 Android sdk 版本 29 开发一个应用程序。我想在我的 Fragments Activity 之一中添加 Google 地图,但它不起作用,我无法修复它。
fradment_map.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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.jHumildes.beautyappointment.Fragments.MapFragment">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mapAPI"
android:name="com.google.android.gms.maps.SupportMapFragment"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
MapFragment.java
public class MapFragment extends Fragment implements OnMapReadyCallback {
GoogleMap mapAPI;
SupportMapFragment mapFragment;
public MapFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
return view;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.mapAPI);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mapAPI = googleMap;
LatLng Dublin = new LatLng(53.3581716, -6.2595678);
mapAPI.addMarker(new MarkerOptions().position(Dublin).title("Dublin"));
mapAPI.moveCamera(CameraUpdateFactory.newLatLng(Dublin));
}
}
我已经实现了这些库,并且还在清单中添加了使用权限。