I've been struggling w this project for weeks now. Specifically getting the API Keys to work, I'm not sure what I'm doing wrong. My code is as follows:
package com.example.hopefullyfinalgeoguessr;
import android.os.Bundle;
import com.google.android.gms.maps.OnStreetViewPanoramaReadyCallback;
import com.google.android.gms.maps.StreetViewPanorama;
import com.google.android.gms.maps.StreetViewPanoramaView;
import com.google.android.gms.maps.SupportStreetViewPanoramaFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.material.snackbar.Snackbar;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.example.hopefullyfinalgeoguessr.databinding.ActivityMainBinding;
import android.view.Menu;
import android.view.MenuItem;
import java.util.Random;
public class MainActivity extends AppCompatActivity implements OnStreetViewPanoramaReadyCallback {
private StreetViewPanorama streetViewPanorama;
private boolean secondlocation = false;
private static final double MIN_LATITUDE = -90;
private static final double MAX_LATITUDE = 90;
private static final double MIN_LONGITUDE = -180;
private static final double MAX_LONGITUDE = 180;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportStreetViewPanoramaFragment streetViewPanoramaFragment = (SupportStreetViewPanoramaFragment)getSupportFragmentManager().findFragmentById(R.id.googlemapstreetview);
streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);
}
@Override
public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
Random random = new Random();
double lat = MIN_LATITUDE + (MAX_LATITUDE - MIN_LATITUDE) * random.nextDouble();
double lng = MIN_LONGITUDE + (MAX_LONGITUDE - MIN_LONGITUDE) * random.nextDouble();
LatLng randomLocation = new LatLng(lat,lng);
this.streetViewPanorama.setPosition(randomLocation);
}
}
manifest xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Hopefullyfinalgeoguessr"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Hopefullyfinalgeoguessr.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="com.google.android.maps.API_KEY"
android:value="redacted" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="redacted" />
<meta-data
android:name="com.google.android.streetview.API_KEY"
android:value="redacted" />
</activity>
</application>
</manifest>
layout xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:id="@+id/googlemapstreetview"
android:name="com.google.android.gms.maps.SupportStreetViewPanoramaFragment" />
</RelativeLayout>
I tried GPT to walk me through this and several YouTube tutorials. I was expecting my street view to load onto the emulator however this is the error message I'm receiving in my logcat:
Caused by: java.lang.RuntimeException: API key not found. Check that <meta-data android:name="com.google.android.geo.API_KEY" android:value="your API key"/> is in the <application> element of AndroidManifest.xml
I think that it's due to my project not recognzing the API.