0

我正在使用 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));
    }
}

我已经实现了这些库,并且还在清单中添加了使用权限。

4

1 回答 1

0
implementation 'com.google.android.gms:play-services-maps:11.0.4'
implementation 'com.google.maps.android:android-maps-utils:0.4+'

...     
<meta-data
                android:name="com.google.android.gms.version"
                android:value="@integer/google_play_services_version" />
            <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="AIzaSyDI-Hx6qi9dEkuGZL_c2sMMg-Cm2wd7wNg" />
        </application>

地图片段

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout_editor_absoluteX="58dp"
        tools:layout_editor_absoluteY="257dp" />

地图类:

 SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.onCreate(savedInstanceState);
        mapFragment.onResume();


  try {
            MapsInitializer.initialize(getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        mapFragment.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap mMap) {
                googleMap = mMap;
                googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                    public void onMapClick(LatLng point) {
                        // Drawing marker on the map
                    }
                });

                googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

                    @Override
                    public void onInfoWindowClick(Marker marker) {
                        // TODO Auto-generated method stub
                    }
                });

                googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(final Marker marker) {

                                DetailData(String.valueOf(marker.getTag()));
                                tag = String.valueOf(marker.getTag());
                                name = marker.getTitle();


                        return false;
                    }
                });
            }
        });
于 2020-05-09T23:02:22.767 回答