Я хочу создать карту Google с маркером. Это относится не к основному виду деятельности, а к другому виду деятельности. Мое приложение продолжает падать, когда я запускаю его (оно падает, только если я выбрал GoogleMapActivity на панели инструментов своего приложения, остальное работает просто отлично).
Это код этой активности
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class GoogleMapActivity extends Fragment implements OnMapReadyCallback {
View v;
GoogleMap mapAPI;
SupportMapFragment mapFragment;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.activity_iss, container, false);
mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapAPI);
mapFragment.getMapAsync(this);
return v;
}
@Override
public void onMapReady(GoogleMap googleMap) {
mapAPI = googleMap;
LatLng test = new LatLng(19.389137, 76.031094);
mapAPI.addMarker(new MarkerOptions().position(test).title("test"));
mapAPI.moveCamera(CameraUpdateFactory.newLatLng(test));
}
}
Это код xml файла
<?xml version="1.0" encoding="utf-8"?>
<!--drawerLayout is top-level container-->
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="3dp"
android:layout_marginEnd="3dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
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"
android:orientation="vertical"
android:background="#000000">
<fragment
android:layout_width="match_parent"
android:layout_height="500dp"
android:id="@+id/mapAPI"
android:name="com.google.android.gms.maps.SupportMapFragment">
</fragment>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Я думаю, что добавил все необходимые мне зависимости в папку Gradle и папку манифеста. Я добавил это в свою папку Gradle:
implementation 'com.google.android.gms:play-services-maps:16.0.0'
, и я добавил это в свою папку манифеста:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
и внутри части приложения в манифесте, у меня есть добавил это, что относится к моему API-ключу Google Maps:
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="@string/map_key"></meta-data>
Изображение logcat: Logcat
случай панели инструментов mainActivity:
case R.id.GoogleMapMenu:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new GoogleMapActivity())
.commit();
break;