Issues with Google Streetview API and xml (Android)

42 Views Asked by At

Sadly, I'm having a lot of trouble finding online resources for what I need on google and GPT is just talking to me in circles at this point. Anyways, I'm trying to integrate the street view API into my project so that the random location my pins drop on show the streetview of it. However, I'm not very sure of how to do this. My relevant code is as follows (apologies for poor formatting my brain is fried from this project):

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;
    private GoogleMap myMap;
    private Geocoder geocoder;
    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;



 u/Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         String geocodingApiKey = getResources().getString(R.string.com_google_android_geo_API_KEY_value1);
         String mapsApiKey = getResources().getString(R.string.com_google_android_geo_API_KEY_value2);


         binding = ActivityMainBinding.inflate(getLayoutInflater());
         setContentView(binding.getRoot());

         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
         mapFragment.getMapAsync(this);
         setSupportActionBar(binding.toolbar);




     }

    u/Override
     public boolean onCreateOptionsMenu(Menu menu) {
     // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
 }

    u/Override
     public boolean onOptionsItemSelected(MenuItem item) {
 // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();

 //noinspection SimplifiableIfStatement
         if (id == R.id.action_settings) {
         return true;
 }

        return super.onOptionsItemSelected(item);
 }

    u/Override
     public void onMapReady(@NonNull GoogleMap googleMap) {
         myMap = googleMap;

         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);


 Geocoder geocoder = new Geocoder(this, Locale.getDefault());
 ArrayList<Address> addresses = null;
        try {
            addresses = (ArrayList<Address>) geocoder.getFromLocation(lat, lng, 1);
            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                String locality = address.getLocality();
                String adminArea = address.getAdminArea();
                String countryName = address.getCountryName();
                String postalCode = address.getPostalCode();
 // use the address information as needed
             }
        } catch (IOException e) {
            e.printStackTrace();
 } catch (IllegalArgumentException e) {
            e.printStackTrace();
 }

// street view

        //add marker w address as title
         String addressString = addresses.get(0).getAddressLine(0);
         myMap.addMarker(new         MarkerOptions().position(randomLocation).title(addressString));
         myMap.moveCamera(CameraUpdateFactory.newLatLng(randomLocation));

 // myMap.addMarker(new MarkerOptions().position(randomLocation).title("Sydney"));
        // myMap.moveCamera(CameraUpdateFactory.newLatLng(randomLocation));


 }

    public void showStreetView() {

   }

}

And for the xml code it is currently saying that my fragments are unknown:

<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout 

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">         

<androidx.fragment.app.FragmentContainerView 

android:layout_width="match_parent" 

android:layout_height="match_parent" android:id="@+id/map" 

tools:context=".MapsActivity" 

android:name="com.google.android.gms.maps.SupportMapFragment" /> 

<androidx.fragment.app.FragmentContainerView 

android:id="@+id/streetviewpanorama_fragment" 

android:name="com.google.android.gms.maps.SupportMapFragment" 

android:layout_width="match_parent" 

android:layout_height="match_parent" /> 

<com.google.android.gms.maps.StreetViewPanoramaView 

android:id="@+id/street_view_panorama" 

android:layout_width="match_parent" 

android:layout_height="match_parent" android:visibility="gone" /> 

<com.google.android.material.appbar.AppBarLayout 

android:layout_width="match_parent" 

android:layout_height="wrap_content" 

android:theme="@style/Theme.TheRealFinalGeoGuesser.AppBarOverlay">     

<androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" 

android:layout_width="match_parent" android:layout_height="?

attr/actionBarSize" android:background="?attr/colorPrimary" 

app:popupTheme="@style/Theme.TheRealFinalGeoGuesser.PopupOverlay" /> 
</com.google.android.material.appbar.AppBarLayout>  

<include layout="@layout/content_main" /> 

</androidx.coordinatorlayout.widget.CoordinatorLayout>

I wasn't expecting anything with the street view API related issue as I'm currently stuck w nowhere to go on that. In fact, I'm not even sure if the API is imported properly (these APIs have been kicking my butt as somebody new to coding). In regards to the xml, I was expecting the fragments to be resolved. I've tried using them as FrameLayouts but it appears as though fragments are preferable for what I'm trying to accomplish (display both an interactive google maps panel and street view panel on the same screen).

0

There are 0 best solutions below